Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed on OS X - can't seem to use + in regexps

Tags:

regex

macos

sed

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

like image 874
stib Avatar asked Aug 04 '09 12:08

stib


2 Answers

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]* 
like image 119
paxdiablo Avatar answered Sep 23 '22 14:09

paxdiablo


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.

like image 27
laalto Avatar answered Sep 24 '22 14:09

laalto