OSX grep – Using Look Ahead

command linemacos

This works:

$ echo "$Foo" | grep "Android SDK Build-tools" | grep -Eo "\d+(?:-)"
4-
5-
6-
7-
8-
9-

This does not:

$ echo "$Foo" | grep "Android SDK Build-tools" | grep -Eo "\d+(?=-)"
grep: repetition-operator operand invalid

Can I use lookahead if I want to with the OSX supplied version of grep?

Best Answer

grep in macOS does not support lookahead. For more information about the regex syntax supported in the default macOS binaries, see re_format(7).

If you need to use such a regex, you can install GNU grep with Homebrew (brew install grep) and use the -P option to enable Perl regex syntax which does support your regex.