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?
My research of the major syntax and functionality differences from http://www.greenend.org.uk/rjk/tech/regexp.html:
.
in GNU grep does not match null bytes and newlines (but does match newlines when used with --null-data
), while Perl, everything except \n
is matched.[...]
in GNU grep defines POSIX bracket expressions, while Perl uses "character" classes. I'm not sure on the details. See http://www.greenend.org.uk/rjk/tech/regexp.html#bracketexpression?
, +
, {
, |
, (
, and )
lose their special meaning; instead use the backslashed versions \?
, \+
, \{
, \|
, \(
, and \)
." From https://www.gnu.org/software/grep/manual/html_node/Basic-vs-Extended.html. ERE matches PCRE syntax.\w
and \W
are the same as [[:alnum:]]
and [^[:alnum]]
, while Perl uses alphanumeric and underscore.\<
and \>
for start and end of word.Perl supports much more additional functionality:
re{...}?
\A
, \C
, \d
, \D
, \G
, \p
, \P
, \s
, \S
, \X
. \Z
, \z
.(?#comment)
(?:re)
, shy grouping + modifiers (?modifiers:re)
(?=re)
and (?!re)
, lookbehind and negative lookbehind (?<=p)
and (?<!p)
(?>re)
(?(cond)re)
man pcresyntax
For other engines, see Regular Expression Engine Comparison Chart by CMCDragonkai