This seems like it should be easy but I can't figure out which permutation of regex matching will result in extracting the whole string after the first number if the string. I can extract the string before the first number like so:
gsub( "\\d.*$", "", "DitchMe5KeepMe" )
Any idea how to write the regex pattern such that the string after the first number is kept?
Instead of lazy dot matching, I'd rely on a \D
non-digit character class and use sub
to make just one replacement:
sub( "^\\D*\\d", "", "DitchMe5KeepMe" )
Here,
^
- matches the start of a string\D*
- matches zero or more non-digits\d
- matched a digitNOTE: to remove up to the first number, add a +
after the last \d
to match one or more digits.
See the IDEONE demo.
What I can see is that you want to remove everything until the first number, so you can use this regex and replace it with an empty string:
^.*?\d
I used .*?
to make the pattern ungreedy, so if you had DitchMe5Keep8Me
it will match DitchMe5
, if you use a greedy pattern like .*\d
it would match DitchMe5Keep8
Regex 101 Demo
R Fiddle Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With