Free Regex cheat sheet

Regex cheat sheet

Every regex token you keep forgetting, on one page: classes, anchors, quantifiers, groups, lookarounds, and flags.

Character classesAnchors and boundariesQuantifiersGroups and alternationLookaroundFlagsCommon patterns

← All cheat sheets

Regex Cheat Sheet

Quick reference from FreeCodingCourses.com

Character classes

.
any character except a newline.
\d \D
a digit, and any non-digit.
\w \W
word char [A-Za-z0-9_], and its opposite.
\s \S
whitespace, and any non-whitespace.
[abc]
any one of a, b, or c.
[^abc]
any character except a, b, or c.
[a-z0-9]
combine ranges in one class.

Anchors and boundaries

^
start of the string (or line with the m flag).
$
end of the string (or line with the m flag).
\b
a word boundary.
\B
not a word boundary.
\bcat\b
matches cat but not category.
^\d+$
the whole string is digits only.

Quantifiers

a*
zero or more a.
a+
one or more a.
a?
zero or one a (optional).
a{3}
exactly three a.
a{2,4}
between two and four a.
a{2,}
two or more a.
.*?
lazy: match as few characters as possible.

Groups and alternation

(abc)
capturing group, saved as $1 or \1.
(?:abc)
group without capturing.
(?<year>\d{4})
named group, read back as year.
cat|dog
matches cat or dog.
(cat|dog)s
group the alternatives, then s.
\1
backreference to the first group.

Lookaround

x(?=y)
x only if followed by y (lookahead).
x(?!y)
x only if not followed by y.
(?<=y)x
x only if preceded by y (lookbehind).
(?<!y)x
x only if not preceded by y.
\d+(?= dollars)
the number right before ' dollars'.
(?<=\$)\d+
the number right after a $ sign.

Flags

g
global: find all matches, not just the first.
i
case-insensitive.
m
multiline: ^ and $ match each line.
s
dotall: . also matches a newline.
/pattern/gi
a JavaScript literal with flags.
re.compile(p, re.I)
flags in Python via the re module.

Common patterns

^\S+@\S+\.\S+$
rough email shape, not full validation.
\d{4}-\d{2}-\d{2}
an ISO date like 2026-07-13.
https?://\S+
a URL starting with http or https.
^\d{3}-\d{3}-\d{4}$
a US phone like 555-123-4567.
\s+
runs of whitespace, handy for splitting.
#[0-9a-fA-F]{6}
a six-digit hex color code.

Unlock the full Regex cheat sheet

You're seeing 2 of 7 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

Regex questions, answered

Does the same regex work in every language?

Mostly, for the core syntax. Character classes, anchors, quantifiers, and groups behave the same in JavaScript, Python, Java, PHP, and PCRE. The differences show up at the edges: lookbehind support, named group syntax, and how you pass flags. Test your pattern in the language you are actually using before you ship it.

What is the difference between greedy and lazy matching?

A greedy quantifier like .+ matches as much as it can, then backtracks if the rest of the pattern fails. A lazy quantifier like .+? matches as little as it can and expands only when needed. On HTML tags or quoted strings, greedy often grabs too much, so reach for the lazy version.

How do I match text across multiple lines?

Two different flags do two different jobs. The m flag makes ^ and $ match at the start and end of each line instead of the whole string. The s (dotall) flag makes the dot match newline characters too. You often want both when a pattern should span line breaks.

Where to go next