Perl and Emacs regular expressions compared
This page is intended as a memory-jogger about the different syntax of
Perl and Emacs regular expressions. The Emacs regular expressions
described here are the command-line regular expressions used in things
like replace-regexp
rather than the elisp regular
expressions.
Perl regex | Emacs regex | Notes |
---|---|---|
(, ) | (, \) | Emacs uses backslashes in the style of "grep" for capturing parentheses, whereas Perl uses the "egrep" style of capturing parentheses without backslashes. |
{, } | {, \} | Emacs uses backslashes in the style of "grep" for curly brackets used for the number of matches, whereas Perl uses the "egrep" style without backslashes. |
s | s- | s matches whitespace in Perl. In Emacs, \s- matches the whitespace character class. The \s designates a character class and the minus sign makes it the "whitespace" class. |
$1, $2, $3, ... | 1, \2, \3, ... | Emacs puts the results of capturing parentheses into \1, \2, etc. Perl uses a dollar sign, as in $1, $2, etc. |
[, ] | [, ] | Emacs and Perl use the same characters, [ and ], to create character classes. |
d | [0-9] | Emacs does not have a way to match digits except by using posix
character classes, as in [[:digits:]] . |
b | b | Perl and Emacs use the same \b to match word boundaries. |
w | w | Perl and Emacs use the same \w character to match word characters. |
quotemeta | regexp-quote |
Regex Tool is a tool for showing what matches a regex inside Emacs. It works with both Emacs and Perl regular expressions. There is also something called "regexp-builder" in Emacs, but this actually uses a different form of regular expressions (the ones for Emacs Lisp) and so it is not very useful in practice.
Web links
-
Question about "comparison table for emacs regexp and perl compatible regular expression (PCRE)?" - stackoverflow.com
A little information on differences at a community website.
-
RegularExpression - Emacs Wiki
A very in-depth guide to Emacs regular expressions, which gets to the point much more rapidly than the meandering Emacs documentation.
-
Emacs Lisp for Perl Programmers
A table comparing Emacs Lisp and Perl has a little information about regular expressions.