To help myself learn Emacs regular expressions, I've put together this cheat sheet comparing regular expression syntax in Emacs, Python, and egrep.
Note that in Emacs, the syntax described below is for regular expressions that are entered directly (e.g. in isearch-forward-regexp and occur). When you provide a regexp in a string (e.g. in Lisp code or in re-builder) you need to double all backslashes.
Emacs | Python | egrep | |
Any character | . | . | . |
Beginning of line | ^ | ^ | ^ |
End of line | $ | $ | $ |
0 or more repetitions | * | * | * |
1 or more repetitions | + | + | + |
3-5 repetitions | \{3,5\} | {3,5} | {3,5} |
Optional | ? | ? | ? |
Character set | [...] | [...] | [...] |
Alternatives | \| | | | | |
Group | \(...\) | (...) | (...) |
Named group | (?P<name>...) | ||
Non-capturing group1 | \(?:...\) | (?:...) | |
Word boundary | \b | \b | \b |
Digit | [[:digit:]] | \d | [[:digit:]] |
Whitespace char | \s- | \s | |
Alphanumeric char | \w | \w | \w |
Back reference | \1 | \1 | |
Named back reference | (?P=name) |
1 Also referred to as "shy groups".