1

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?

2

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.

  • Note if you're writing scripts to work with a vanilla install of macOS [[ "$OSTYPE == darwin* ]] you should make it work with BSD grep and not expect the user to have GNU grep installed already. – Josh Habdas Oct 18 at 11:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .