Regex Cheat Sheet

A searchable reference of common regular expression syntax — character classes, anchors, quantifiers, groups, lookaround, and flags — with examples.

CategoryPatternDescriptionExample
Character classes
.Any character except newlinea.c matches abc
Character classes
\dDigit (0-9)\d+ matches 123
Character classes
\DNon-digit\D+ matches abc
Character classes
\wWord character (letters, digits, underscore)\w+ matches hello_1
Character classes
\WNon-word character\W matches @
Character classes
\sWhitespace\s+ matches spaces/tabs
Character classes
\SNon-whitespace\S+ matches non-space run
Character classes
[abc]Any of a, b, or c[abc] matches a, b, or c
Character classes
[^abc]Anything except a, b, or c[^abc] matches d, e, ...
Character classes
[a-z]Any lowercase letter (range)[a-z]+ matches hello
Anchors
^Start of string (or line with m flag)^abc matches abc at start
Anchors
$End of string (or line with m flag)abc$ matches abc at end
Anchors
\bWord boundary\bcat\b matches whole word cat
Anchors
\BNon-word boundary\Bcat matches concatenate
Quantifiers
*0 or moreab*c matches ac, abc, abbc
Quantifiers
+1 or moreab+c matches abc, abbc
Quantifiers
?0 or 1 (optional)ab?c matches ac or abc
Quantifiers
{n}Exactly n timesa{3} matches aaa
Quantifiers
{n,}n or more timesa{2,} matches aa, aaa, ...
Quantifiers
{n,m}Between n and m timesa{2,4} matches aa to aaaa
Quantifiers
*?0 or more, lazy (non-greedy)<.+?> matches shortest tag
Groups
(abc)Capturing group(ab)+ captures repeated ab
Groups
(?:abc)Non-capturing group(?:ab)+ groups without capturing
Groups
(?<name>abc)Named capturing group(?<year>\d{4})
Groups
a|bAlternation (OR)cat|dog matches cat or dog
Lookaround
(?=abc)Positive lookahead\d+(?=px) matches digits before px
Lookaround
(?!abc)Negative lookahead\d+(?!px) digits not before px
Lookaround
(?<=abc)Positive lookbehind(?<=\$)\d+ digits after $
Lookaround
(?<!abc)Negative lookbehind(?<!\$)\d+ digits not after $
Flags
gGlobal — find all matches/abc/g
Flags
iCase-insensitive/abc/i matches ABC
Flags
mMultiline — ^ and $ match line boundaries/^abc/m
Flags
sDot matches newline too/a.b/s
Flags
uUnicode mode/\u{1F600}/u