I would like to know if, for instance, \{x,y\}
in sed
will try to match as much or as little as possible characters.
Also, can someone explain to me the bellow unexpected behaviour of sed
?
echo "baaab" | sed 's/a\{1,2\}//'
bab
echo "baaab" | sed 's/a\{0,2\}//'
baaab
In the first line, sed
becomes greedy, in the second apparently it doesn't, is there a reason for that?
I'm using GNU sed version 4.2.1.
Best Answer
a\{0,2\}
will match the empty string at the start of the line (actually, any empty string, butg
wasn't specified):Since GNU
sed
does matching from left to right, and a global replacement wasn't specified, only the start of the line matched. If you'd usedg
:The empty strings at the start and end matched, and the
aa
, and the remaininga
.