Standard sed
only understands POSIX Basic Regular Expressions (BRE), not Extended Regular Expressions (ERE), and the ?
is a metacharacter in EREs, but not in BREs.
Your version of sed
might support EREs if you turn them on. With GNU sed
, the relevant options are -r
and --regexp-extended
, described as "use extended regular expressions in the script".
However, if your sed
does not support it - quite plausible - then you are stuck. Either import a version of sed
that does support them, or redesign your processing. Maybe you should use awk
instead.
2014-02-21
I don't know why I didn't mention that even though sed
does not support the shorthand ?
or ?
notation, it does support counted ranges with {n,m}
, so you can simulate ?
with {0,1}
:
sed -n '/(www.){0,1}teste/p' << EOF
http://www.tested.com/
http://tested.com/
http://www.teased.com/
EOF
which produces:
http://www.tested.com/
http://tested.com/
Tested on Mac OS X 10.9.1 Mavericks with the standard BSD sed
and with GNU sed
4.2.2.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…