Now according to all the literature
echo 1234abcd|sed "s|[0-9]\+|#|g"
should output #abcd. And
echo abcd|sed "s|[0-9]\+|#|g"
should output abcd.
But on OS X 10.4.11 the first expression outputs 1234abcd. Using * instead of + works for the first example but fails on the second, outputting #abcd, because the [0-9] pattern is matched zero times.
Does the + operator not work in regular expressions in OS X? Is there an alternative?
Thanks
On OSX, sed
by default uses basic REs. You should use sed -E
if you want to use modern REs, including the "+"
one-or-more operator.
See here for the indication that sed
uses basic REs by default, here for the modern RE syntax, and here for the basic RE (ed
) information.
Alternatively, if you have a regular expression engine that doesn't support +
at all, you can simply use *
instead, by converting (for example):
[a-z]+
into:
[a-z][a-z]*
Obsolete basic regular expressions do not support +
and ?
quantifiers. They are regular characters.
Alternatives for [0-9]+
are e.g. [0-9]{1,}
or [0-9][0-9]*
.
Or you can use sed -E
to use modern, extended regular expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With