Originally Posted By: Virtual1
I've been unable to get the "?" operator to work in SED. It works fine in GREP. Example:

echo "nathan" | sed "s/na[t]\{0,1\}han/_/g"
_
(that works)

echo "nathan" | sed "s/na[t]\?han/_/g"
nathan
(that doesn't work)

/? and /{0,1\} should be functionally identical, zero or one matches of the preceding pattern. Both work in GREP, but only the range one works in SED, and I can't find any explanation for why. I've tried different variations of escaping, quoting, and options like -E and -e.

There are 2 classes of regular expressions: BRE and ERE (basic and extended).

? by itself has no special meaning in BRE.
Nor does + nor | nor ( nor )

the original grep didn't support ERE either... so egrep was developed.
nowadays grep can do both by virtue of the -E flag ... and the same should now be true for sed as well.

i.e., try sed -E

[unless you post examples of the the sed -E expression you've already tried... we can't see what went wrong.]

Last edited by Hal Itosis; 09/30/11 06:20 PM.