Blog
The pocket regex cheat sheet (bookmark this)
Every regex token you actually need on one page. Print, bookmark, or pin to your editor.
Anchors
^ start of string (or line with /m)
$ end of string (or line with /m)
\b word boundary
\B not a word boundary
Character classes
. any char (not newline)
[abc] a, b, or c
[^abc] not a, b, or c
[a-z] range a through z
\d digit [0-9]
\D non-digit
\w word char [A-Za-z0-9_]
\W non-word char
\s whitespace
\S non-whitespace
Quantifiers
* 0 or more
+ 1 or more
? 0 or 1
{n} exactly n
{n,} n or more
{n,m} n to m
*? lazy version (smallest match)
+? lazy version
Groups
(abc) capture group
(?:abc) non-capturing group
(?<name>abc) named group
\1 back-reference to group 1
\k<name> named back-reference
Lookarounds
(?=...) positive lookahead (followed by)
(?!...) negative lookahead (NOT followed by)
(?<=...) positive lookbehind (preceded by)
(?<!...) negative lookbehind (NOT preceded by)
Flags
g global (find all matches)
i case-insensitive
m multiline (^ $ at line boundaries)
s dotall (. matches newlines)
u unicode (JS)
x verbose mode (most flavors, not JS)
Replacement metacharacters
$& the entire match
$1 capture group 1
$<name> named group
$$ literal dollar sign
Escape these to use literally
. * + ? ^ $ | ( ) [ ] { } \
Want a printable PDF? Open the cheat sheet and use your browser's print to save as PDF.