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 regexEmacs regexNotes
(, ) (, \) 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

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer