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.