In R, I need to extract "Eight" from the following string:
this_str <- " Eight years blah blah 50 blah blah, two years blah blah blah."
Here is my attempt using gsub:
gsub("^.*\\s([^ ]*)\\s(years|months)\\s.*", "\\1", this_str)
But this returns "two", which corresponds to the second occurrence of the pattern indicated in gsub(). In other posts it is said that sub() should return the first match. But when I use sub() it also gives "two".