So let's say I have a string
"Happy 2022 New 01 years!"
I'm looking to return the "01". To be more specific, I need the last set of digits in the string. This number could just be '1', or '10', or '999'... The string otherwise could be pretty much anything. I tried various regex with gsub, but can't seem to get it just right. There is something I misunderstood.
Eg, If I do this:
gsub('.*(\\d+).*$', '\\1', x)
Then why do I get back "1"? Does the '+' in the regex not specify one or more digits?
How is my interpretation wrong?: '.' for any characters, '(\\d+)' for one or more digits, '.'for some more characters, '$' at the end of the string. gsub is greedy, so it will return the last set of digits (therefore '01', not '2022'). '\\1' will replace the whole string with the first, and only, match. x is the string.