Search code examples
regexgrepgnupcre

What are the differences between GNU grep's basic/extended and PCRE (`-P`) regular expressions?


GNU grep's basic (BRE) and extended (ERE) syntax is documented at https://www.gnu.org/software/grep/manual/html_node/Regular-Expressions.html and PCRE is summarized at man pcresyntax, but there is no explicit comparison. What are the differences between GNU grep's basic/extended and PCRE (-P) regular expressions?


Solution

  • My research of the major syntax and functionality differences from http://www.greenend.org.uk/rjk/tech/regexp.html:

    Perl supports much more additional functionality:

    • "nongreedy {}" with syntax re{...}?
    • additional anchors and character types \A, \C, \d, \D, \G, \p, \P, \s, \S, \X. \Z, \z.
    • (?#comment)
    • shy grouping (?:re), shy grouping + modifiers (?modifiers:re)
    • lookahead and negative lookahead (?=re) and (?!re), lookbehind and negative lookbehind (?<=p) and (?<!p)
    • Atomic groups (?>re)
    • Conditional expression (?(cond)re)
    • ... and more, see man pcresyntax

    For other engines, see Regular Expression Engine Comparison Chart by CMCDragonkai