I am using sed. I was using a regex that was correct as far as I could see, but sed did not do anything. Turns out that I was using \s+ which sed can not understand, and when I switched to [ ]+ it worked.

So to sum up, I made a regex which for it to work I had to escape almost everything and remove the \s for whitespace. Seems that there is a mode to avoid all these which is -r So I wanted to ask:

  1. Why isn't -r the default mode for sed? Why did I have to go to so much trouble to escape everything?
  2. man says that there is the option --posix and that "POSIX.2 BREs should be supported" What does this refer to? NFA/DFA mode?
  • As an aside, to match whitespace like perl's \s+ use [[:space:]][:space:]]* in BRE and the same or [[:space:]]+ in ERE.
    – jrm
    Apr 1 '16 at 3:35

Re 1) The answer is the same as for any other tool that was improved over decades. :)

You don't want to break existing scripts by changing default behaviour.

Re 2) That has nothing to do with the matching engine; it's just a question of which set of regular expressions is supported. POSIX BRE means "basic regular expression".


Regarding point 2: gnu sed uses GNU BRE by default. To use POSIX BRE you should specify --posix

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.