32

I'm guessing it's not a Perl Compatible Regular Expression, since there's a special kind of grep which is specifically PCRE. What's grep most similar to?

Are there any special quirks of grep that I need to know about? (I'm used to Perl and the preg functions in PHP)

2
  • 2
    It is worth noting that grep (and its several flavors of regexs) predates perl and php by a considerable period. The question isn't "Why doesn't grep do what per does?" but "Why did perl choose to do something different that grep?". Commented Jul 2, 2009 at 16:02
  • 1
    How do the answers on this page map to the options available in regex101.com? I'd like to use that site to test my grep regex. The options there are PCRE2, PCRE ECMA Python Golang Java 8 Commented Feb 25, 2022 at 17:39

6 Answers 6

32

Default GNU grep behavior is to use a slightly flavorful variant on POSIX basic regular expressions, with a similarly tweaked species of POSIX extended regular expressions for egrep (usually an alias for grep -E). POSIX ERE is what PHP ereg() uses.

GNU grep also claims to support grep -P for PCRE, by the way. So no terribly special kind of grep required.

2
  • I think GNU grep just support GNU BRE and GNU ERE. Because according to regular-expressions.info/refflavors.html POSIX BRE and POSIX ERE don’t support \w but GNU grep does.
    – Gumbo
    Commented Jul 2, 2009 at 15:24
  • 1
    The "special" grep he's referring to might be ack, which deserves mention.
    – Chris Lutz
    Commented Jul 8, 2009 at 23:04
9

POSIX BRE (Basic Regular Expressions)

You can compare the various flavors here.

0
8

There's a good write-up here. To quote the page, "grep is supposed to use BREs, except that grep -E uses EREs. (GNU grep fits some extensions in where POSIX leaves the behaviour unspecified)."

In other words, it's a long story. ;)

3

Grep is an implementation of POSIX regular expressions. There are two types of posix regular expressions -- basic regular expressions and extended regular expressions. In grep, generally you use the -E option to allow extended regular expressions.

1

The grep man pages do a pretty thorough job of explaining the flavor of regexp available in grep. man grep is pretty useful.

0
0

There is no regular grep function in PHP. If you are referring to the ereg family of PHP functions then those are POSIX regular expressions. If you are referring to the Linux grep commandline utility, those are POSIX regular expressions as well. It supports both basic as well as extended POSIX regular expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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