I am having trouble using regular expressions to extract a longitude and latitude from a string. The string is this:
[1] "\"42.352800\" data-longitude=\"-71.187500\" \"22\"></div>"
I want to be able to get both the first number "42.352800" and the second number "-71.187500" separately as two variables. Because I'll be doing this on a bunch of entries, I need to make sure that it can get these numbers whether they are positive or negative.
I figured I should be using a regular expression to say basically:
latitude <- from " to " (to get the first number)
and then something similar to get the longitude.
Any ideas here? I am relatively new to regex.
I agree with @r2evans that if you are scraping this information from a webpage it would be much simpler to get data using rvest
for example.
To answer your question, you can use str_match
to get first two numbers.
string <- "\"42.352800\" data-longitude=\"-71.187500\" \"22\"></div>"
stringr::str_match(string, '(\\d+\\.\\d+).*?(-?\\d+\\.\\d+)')[, -1]
#[1] "42.352800" "-71.187500"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.