Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R vs sed regex greediness

Tags:

regex

r

sed

I don't quite understand why this doesn't result in "test" and would appreciate an explanation:

a = "blah test"
sub('^.*(test|$)', '\\1', 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)|^.*', '\\1', a)
like image 827
eddi Avatar asked Mar 24 '23 03:03

eddi


1 Answers

You need to mark the ^.* as non-greedy

> sub('^.*?(test|$)', '\\1', "blah test")
[1] "test"
> sub('^.*?(test|$)', '\\1', "blah blah")
[1] ""
like image 51
GSee Avatar answered Apr 06 '23 14:04

GSee