0

Are sed matches always greedy?

I am trying to use sed to append an XML comment into an XML file as early as possible, which is right after the XML declaration if there is one. Something like this, in shell environment:

sed -e 's/\(<?xml[^?]*?>\)\?$/\1<!-- This is the new comment -->/'

I understand that sed wants to find the leftmost match and be as greedy as possible in the leftmost location where a match starts. However, the optional group doesn't match any XML declaration like this:

<?xml version="1.0" encoding="utf-8"?>

and instead it chooses to match an empty string, so the comment gets appended before the XML declaration. Why is that?

If I make the group mandatory, it matches as expected.

0

Ah. The document started with a UTF-8 BOM.

Therefore the empty string match was happening before the BOM, while the "expected" match after the BOM wasn't the leftmost possible match.

2
  • So, the answer is basically "yes" ;)
    – AdminBee
    Jan 16 '20 at 8:44
  • @AdminBee - The answer is that groups are reliably greedy like any other regex construct. But leftmostness trumps greed. And optional groups may end up matched in very obscure places - in mine, there were 3 distinct matches to the left of the expected match site! Jan 16 '20 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.