'git grep' and word boundaries on Mac OS X and BSD
git grep "\<blah\>" regularly on my linux development server, but I just discovered that I am not able to use
\> on Mac (Mac OS X 10.6.8) (not able to use = it does not find anything). Is the regular expressions syntax different in Mac?
I tried using
git grep -E "\<blah\>" but to no avail! 🙁
3 Solutions collect form web for “'git grep' and word boundaries on Mac OS X and BSD”
I guess it’s caused by the BSD vs Linux grep library.
See if the -w (match pattern only at word boundary) option to git grep does it for you:
$ git grep -w blah
You can compile git with
PCRE support and use
git grep -P "\bblah\b" for word boundaries.
Here’s a guide on how to compile git using OSX Homebrew:
After struggling with this, too, I found this very helpful post on a BSD mailing list. So here’s the (albeit rather ugly) solution:
git grep "[[:<:]]blah[[:>:]]"
-w flag of git-grep also works but sometimes you want to only match the beginning or end of a word.
Update: This has changed in OS X 10.9 “Mavericks”. Now you can use
[[:>:]] are no longer supported.