R vs sed regex greediness
I don’t quite understand why this doesn’t result in "test"
and would appreciate an explanation:
a = "blah test"
sub('^.*(test|$)', '', a)
# [1] ""
Compare it to the sed
expression:
echo 'blah test' | sed -r 's/^.*(test|$)/1/'
# test
echo 'blah blah' | sed -r 's/^.*(test|$)/1/'
#
Fwiw, the following achieves what I want in R (and is equivalent to the above sed
results):
sub('^.*(test)|^.*', '', a)
You need to mark the ^.*
as non-greedy
> sub('^.*?(test|$)', '', "blah test")
[1] "test"
> sub('^.*?(test|$)', '', "blah blah")
[1] ""