Is there a nice table or a cheatsheet on the web that compares the sytax of emacs regex and PCRE?
That I have to remember to escape grouping parenthesis and braces and other differences when I'm using emacs regex, it's all confusing, a syntax comparison table would be good for minimizing confusion.
As of Perl 5.10, PCRE is also available as a replacement for Perl's default regular-expression engine through the re::engine::PCRE module. The library can be built on Unix, Windows, and several other environments.
PCRE tries to match Perl syntax and semantics as closely as it can. PCRE also supports some alternative regular expression syntax (which does not conflict with the Perl syntax) in order to provide some compatibility with regular expressions in Python, .
grep understands three different versions of regular expression syntax: basic (BRE), extended (ERE), and Perl-compatible (PCRE). In GNU grep , there is no difference in available functionality between the basic and extended syntaxes.
I will collect syntax differences that I know here. This answer is community wiki, add more if anyone wishes. Always check the preview before adding more.
In Emacs regexp, (, ), {, }, | are literal and escaped ones (\(, \), \{, \}, \|) are meta.
In Perl-compatible regexp, (, ), {, }, | are meta, and escaped ones are literal.
\* is the literal star in both Emacs and Perl. If an expression starts with a star, the starting star is literal in Emacs regexp, illegal in Perl regexp.
Similarly for the plus.
The character classes \d
(for digits), \w
(for words), \s
(for whitespace characters) do not work in Emacs regular expressions, but work in Perl. In Emacs, use [[:digit:]]
, [[:word:]]
, [[:space:]]
instead (with double brackets). In Perl, they are also [:digit:]
, [:word:]
, [:space:]
(single brackets).
\w
in Emacs matches '
and "
too, unlike Perl. This is because text-mode syntax table has '
and "
as word characters.
Of backslash constructs mentioned in Emacs Regexp Backslash, the following constructs are NOT in Perl compatible regular expressions.
\` \' \= \< \> \_< \_> \sC \cC
See also what < and > can do that \b cannot do
\A
, \Z
, \z
are NOT in Emacs. In Emacs, use instead:
\` or \'
See the second section in Text Pattern Matching in Emacs. It also mentions why \n
and \t
don't match newlines and tabs in incremental search forward for regular expression (C-M-s
or M-x isearch-forward-regexp
) and what to do.
Emacswiki regular expression
I think you're looking for http://www.regular-expressions.info/refflavors.html
Emacs's regexes are "GNU ERE" in those tables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With