Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to do something like this:

grep -r '(?<!notthis)butthis' ./*

And I'm getting no results when some should be returned. Am I missing an escape? Or does OSX just not support regex lookaround?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The default OS X grep only supports Basic and Extended Regular Expressions as defined in POSIX and explained under re_format(7) for OS X. These flavors do not support negative look-behind.

If you want more powerful regular expression syntax, combined with greps ability to search a directory recursively for a regex pattern, you should consider using ack, which is written in Perl and thus supports its regex syntax, including negative look-behind.

The equivalent with ack would be:

ack '(?<!notthis)butthis'

You can install ack through:


It's worth noting that GNU grep has a -P option to enable Perl compatible regex syntax, however it is not included with OS X – you can install it through Homebrew if you like with brew install grep. On Linux, it will be available by default.

If you're switching between Linux and OS X a lot, like I do, I'd recommend using GNU grep on OS X by default, or using ack as a grep replacement altogether.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.