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.

1 Answer 1


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.

  • So, the answer is basically "yes" ;)
    – AdminBee
    Jan 16, 2020 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, 2020 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .