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.

Is the regular expression standard used in grep POSIX + ASCII or something else is mixed in?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

That all depends on which flags you pass to grep.

The normal flagless grep (which is the same as passing -G) uses "Basic regular expressions":

-G, --basic-regexp
    Interpret PATTERN as a basic regular expression (BRE, see 
    below).  This is the default.

If you specify -E it uses "Extended" regular expressions:

-E, --extended-regexp
    Interpret PATTERN as an extended regular expression (ERE, 
    see below).  (-E is specified by POSIX.)

And then you have -P for Perl regular expressions (PCRE):

-P, --perl-regexp
    Interpret PATTERN as a Perl regular expression.  This is highly 
    experimental and grep -P may warn  of  unimplemented features.

Basic vs Extended Regular Expressions

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, +, {, \|, (, and ).

Traditional egrep did not support the { meta-character, and some egrep implementations support { instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a literal {.

GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification. For example, the command grep -E '{1' searches for the two-character string {1 instead of reporting a syntax error in the regular expression. POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.

So although grep strives to be as close to POSIX as possible there are still some flaws in it.

share|improve this answer
    
Yeah, I rad that but it doesn't say which standard is actually used, and if it's a mix of stuff then there's no referance to the mixed standart. So basically we know that it works with something but don't know with what exactly. :D –  Denys S. Apr 12 '11 at 12:08
    
@den: POSIX regular expressions are explained in regex(7), Perl's - perlre. –  grawity Apr 12 '11 at 12:09
    
This is about Linux, but the "-P" option is not available under MinGW ("grep: Support for the -P option is not compiled into this --disable-perl-regexp binary"), at least not by default. –  Peter Mortensen May 1 '12 at 11:02
    
Different greps understand (slightly) different REs (and flags, and...) –  vonbrand Feb 11 '13 at 2:37
add comment

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.