I have column names like this: var1
, var2
, var3
, …. var99
. but sometimes there are wrong entries between var and the number, something like var dfdsf 22
. I want to extract only the relevant part, which is “var” and the number at the end. What I do is:
gsub("^(var).*(\d+)$", "\1\2", "var X 23")
"var3"
Why does this not return var23
? I thought .
chatches anything and *
says to do so zero or more times. It works if I do gsub("^(var) .* (\d+)$", "\1\2", "var X 23")
. But if there are no spaces between the random characters and the numbers at the end then this does not work anymore, see gsub("^(var) .* (\d+)$", "\1\2", "var X23")
. How to adapt the pattern so that I will always get var23
, no matter what characters are between the var and the number?