Sharath Sharath - 1 year ago 132
R Question

Extract date from a given string in R

Here is a string that I have


I am trying to extract dates this way:


I am getting NA for this.

Desired output is


Can someone point me in the right direction?

Answer Source

You have replaced your dash - with a slash / in your regular expression.

as.Date(str_extract(string, "[0-9]{4}-[0-9]{2}-[0-9]{2}"), format="%Y-%m-%d")
# [1] "2013-08-21"

But you can also replace the [0-9] bits with \d, which represent the same thing. I'm not sure why, but regex pros seem to always use the \d version (note that you'll have to escape the backslash with another backslash):

as.Date(str_extract(string, "\\d{4}-\\d{2}-\\d{2}"), format="%Y-%m-%d")
# [1] "2013-08-21"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download