Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 212013

Find nearest number to specific word using regex in R [closed]

$
0
0

I want to find the nearest number between a specific word in a string. Lets say the word is "week" (FYI: since "week" is a subset of "weeks", "weeks" can be used too)

eg1 <- c("100 this is 40 weeks old")
eg2 <- c("this is week 40, 52 years")
eg3 <- c("omg its been 40 blah weeks, not sure though could be 1000")
eg4 <- c("omg its been 40 blah weeks (30 maybe), not sure though could be 1000")
eg5 <- c("Week 20 is not far away")

This is the output I would like

[1] 40
[2] 40
[3] 40
[4] 30
[5] 20

How can I get this using regex in R?

Edit: I added another example, not sure why stackoverflow is asking me to be more focused/specific in the question! I have provided all the details I could.


Viewing all articles
Browse latest Browse all 212013

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>