Regex cheat sheet

Every token you'll reach for, with an example and what it matches. Type to filter; press an example to open it in the tester.

Character classes

TokenMeaningExampleMatches
.Any character except newlinec.tcat, c9t, c t
\dDigit 0–9\d\d42
\DNot a digit\D+abc
\wWord character: A–Z a–z 0–9 _\w+user_1
\WNot a word character\W@ in a@b
\sWhitespace (space, tab, newline…)a\sba b
\SNot whitespace\S+word
[abc]One of a, b or cgr[ae]ygray, grey
[^abc]Any character except a, b, c[^0-9]x
[a-z]Range a to z[A-F0-9]+1F3A
\p{L}Any Unicode letter (needs u flag)\p{L}+Zoë, 東京

Anchors and boundaries

TokenMeaningExampleMatches
^Start of string (line with m)^HiHi there
$End of string (line with m)end$the end
\bWord boundary\bcat\bcat, not in concat
\BNot a word boundary\Bcatconcat
\A \ZStart / end of string (Python, PCRE; not JS)\Aabc\Zabc

Quantifiers

TokenMeaningExampleMatches
*0 or more (greedy)ab*a, ab, abbb
+1 or moreab+ab, abbb
?0 or 1 (optional)colou?rcolor, colour
{n}Exactly n times\d{4}2026
{n,}n or more\d{2,}42, 12345
{n,m}Between n and m\w{3,5}abc, abcde
*? +? ??Lazy: as few as possible<.+?><b> in <b>x</b>
*+ ++Possessive: never give back (PCRE, Java; not JS)\d++123

Groups and references

TokenMeaningExampleMatches
(abc)Capture group(\d+)-(\d+)$1=10, $2=20
(?:abc)Group without capturing(?:ab)+abab
(?<name>…)Named group (Python: (?P<name>…))(?<year>\d{4})groups.year
\1Backreference to group 1(\w)\1ll in hello
\k<name>Named backreference(?<q>['"]).*?\k<q>"hi"
a|bAlternation: a or bcat|dogcat, dog

Lookarounds

TokenMeaningExampleMatches
(?=…)Followed by (lookahead)\d+(?=px)12 in 12px
(?!…)Not followed by\d+(?!px)\b12 in 12em
(?<=…)Preceded by (lookbehind)(?<=\$)\d+5 in $5
(?<!…)Not preceded by(?<!\$)\b\d+5 in €5

Escapes

TokenMeaningExampleMatches
\. \* \\Literal special character\d+\.\d+3.14
\n \r \tNewline, carriage return, tab\r?\nline breaks
\xhhCharacter by hex code\x41A
\uhhhhUnicode code unit\u00e9é
\u{h…}Code point (u flag)\u{1F600}😀

Flags

TokenMeaningExampleMatches
gGlobal: all matches/a/gevery a
iCase-insensitive/abc/iABC
mMultiline: ^ $ per line/^\d/meach line start
sdotAll: . matches newline/a.b/sa\nb
uUnicode mode/\p{Emoji}/u🎉
ySticky: match at lastIndex only/foo/ytokenizers
xVerbose: ignore whitespace, allow # comments (Python re.X, PCRE)(?x) \d+ # digits123

Replacement tokens

TokenMeaningExampleMatches
$1 $2Group number (Python: \1 or \g<1>)(\w+) (\w+) → $2 $1swap words
$<name>Named group (Python: \g<name>)$<year>2026
$&The whole match\d+ → [$&][42]
$` $'Text before / after the match
$$Literal dollar sign\d+ → $$$&$42

Reading a pattern left to right

Take ^(?<user>[\w.+-]+)@([\w-]+\.)+[a-z]{2,}$. Anchor to the start; capture one or more word characters, dots, pluses or hyphens as user; a literal @; then one or more "label followed by a dot" groups; then two or more letters; then the end. Every regex decomposes this way — atoms, each with an optional quantifier, joined in sequence or by |. The tester prints this reading for any pattern you type.

Language specifics live on their own pages: JavaScript regex and Python regex.

Questions

Is regex syntax the same in every language?

The core — classes, quantifiers, groups, anchors, alternation — is shared by JavaScript, Python, PCRE (PHP), Java, .NET, Ruby and Go. Differences are in extras: named-group syntax, lookbehind support, possessive quantifiers, \A/\Z anchors and inline flags. Rows on this sheet note the main exceptions.

What is the difference between greedy and lazy?

Greedy quantifiers (* + ? {n,m}) match as much as possible and then give characters back if the rest of the pattern fails. Lazy versions (*? +? ?? {n,m}?) match as little as possible and take more only when needed.

How do I match a literal special character?

Put a backslash before it: \. \* \+ \? \( \) \[ \] \{ \} \| \^ \$ \\. Inside a character class most of them are literal already; only ] \ ^ and - need care.

Can I print this cheat sheet?

Yes — use your browser's print command. The page prints as clean tables without the navigation.