πŸ“‹ Cheat Sheets

Regex Cheat Sheet β€” Regular Expressions Made Simple


Click any item to expand the explanation and examples. Try patterns live with our Regex Tester.

πŸ”€ Character Classes

. (dot) β€” Any character basics
Matches any single character except newline.
h.t     β†’ matches "hat", "hot", "hit", "h9t"
..      β†’ matches any two characters
Use \. to match a literal dot.
\d \w \s β€” Shorthand classes basics
Built-in character class shortcuts.
\d    β†’ digit [0-9]
\D    β†’ non-digit [^0-9]
\w    β†’ word char [a-zA-Z0-9_]
\W    β†’ non-word char
\s    β†’ whitespace [ \t\n\r\f]
\S    β†’ non-whitespace
Example: \d{3}-\d{4} matches "555-1234".
[abc] β€” Character sets basics
Match any one character in the set.
[aeiou]     β†’ any vowel
[a-z]       β†’ any lowercase letter
[A-Z]       β†’ any uppercase letter
[0-9]       β†’ any digit
[a-zA-Z]    β†’ any letter
[^aeiou]    β†’ any NON-vowel (^ negates)
Example: gr[ae]y matches "gray" and "grey".

πŸ”’ Quantifiers

* + ? β€” Basic quantifiers quantifiers
Control how many times a pattern repeats.
*     β†’ 0 or more    (greedy)
+     β†’ 1 or more    (greedy)
?     β†’ 0 or 1       (optional)
colou?r    β†’ "color" or "colour"
go+gle     β†’ "gogle", "google", "gooogle"...
ab*c       β†’ "ac", "abc", "abbc"...
{n} {n,m} β€” Exact quantifiers quantifiers
Specify exact repetition counts.
{3}       β†’ exactly 3
{2,5}     β†’ between 2 and 5
{3,}      β†’ 3 or more
\d{3}          β†’ "123", "999"
\w{2,4}        β†’ 2 to 4 word characters
[a-z]{5,}      β†’ 5+ lowercase letters
*? +? ?? β€” Lazy quantifiers quantifiers
Add ? after a quantifier to make it lazy (match as few as possible).
# Greedy (default): matches as MUCH as possible
".*"  on  <a>foo</a><b>bar</b>
β†’ matches "<a>foo</a><b>bar</b>"

Lazy: matches as LITTLE as possible

”.*?” on <a>foo</a><b>bar</b> β†’ matches β€œ<a>” then ”</a>” etc.

βš“ Anchors

^ $ β€” Start and end anchors
Match positions, not characters.
^hello     β†’ line starts with "hello"
world$     β†’ line ends with "world"
^exact$    β†’ entire string is "exact"
With the m (multiline) flag, ^ and $ match start/end of each line.
\b β€” Word boundary anchors
Matches the position between a word character and a non-word character.
\bcat\b    β†’ matches "cat" but NOT "category" or "scat"
\bpre      β†’ matches words starting with "pre"
ing\b      β†’ matches words ending with "ing"

πŸ‘₯ Groups & Alternation

(abc) β€” Capturing groups groups
Group patterns and capture matched text.
(foo)          β†’ captures "foo" as group 1
(foo)(bar)     β†’ group 1: "foo", group 2: "bar"
(\d{3})-(\d{4}) β†’ captures area code and number separately
Reference captured groups with \1, \2 in the pattern or $1, $2 in replacements.
(?:abc) β€” Non-capturing groups groups
Group without capturing (better performance).
(?:foo|bar)baz   β†’ matches "foobaz" or "barbaz"
                   but doesn't capture "foo"/"bar"
Use when you need grouping for alternation or quantifiers but don't need the captured value.
| β€” Alternation (OR) groups
Match one pattern or another.
cat|dog         β†’ "cat" or "dog"
(red|blue) car  β†’ "red car" or "blue car"
^(GET|POST|PUT) β†’ line starts with HTTP method
(?<name>) β€” Named groups groups
Give groups meaningful names.
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

In JavaScript:

const m = β€œ2026-03-14”.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/); m.groups.year // β€œ2026” m.groups.month // β€œ03”

\1 \2 β€” Backreferences groups
Reference a previously captured group within the same pattern.
(\w+)\s+\1     β†’ matches repeated words: "the the", "is is"
(["']).*?\1    β†’ matches quoted strings with matching quotes

πŸ‘€ Lookahead & Lookbehind

(?=...) β€” Positive lookahead lookaround
Assert what follows, without including it in the match.
\d+(?=px)      β†’ matches "12" in "12px" but not "12em"
foo(?=bar)     β†’ matches "foo" only if followed by "bar"
(?!...) β€” Negative lookahead lookaround
Assert what does NOT follow.
\d+(?!px)      β†’ matches "12" in "12em" but not "12px"
foo(?!bar)     β†’ matches "foo" NOT followed by "bar"
(?<=...) β€” Positive lookbehind lookaround
Assert what precedes, without including it in the match.
(?<=\$)\d+     β†’ matches "50" in "$50" but not "50"
(?<=@)\w+      β†’ matches username after @ in emails
(?<!...) β€” Negative lookbehind lookaround
Assert what does NOT precede.
(?<!\$)\d+     β†’ matches "50" when NOT preceded by $
(?<!un)happy   β†’ matches "happy" but not "unhappy"

🚩 Flags

g i m s u β€” Regex flags flags
Modify how the regex engine behaves.
g    β†’ global: find all matches, not just the first
i    β†’ case-insensitive: /hello/i matches "Hello", "HELLO"
m    β†’ multiline: ^ and $ match line starts/ends
s    β†’ dotAll: . also matches newline characters
u    β†’ unicode: enable full Unicode support
# JavaScript
/hello/gi          β†’ global + case-insensitive
new RegExp("hello", "gi")

Python

re.findall(r”hello”, text, re.IGNORECASE)

🎯 Common Patterns

Email address patterns
Basic email validation pattern.
[\w.+-]+@[\w-]+\.[\w.]+
Matches: user@example.com, first.last+tag@sub.domain.org
URL patterns
Match HTTP/HTTPS URLs.
https?://[\w\-.]+(:\d+)?(/[\w\-./?%&=]*)?
Matches: https://example.com, http://localhost:3000/path?q=1
Phone number patterns
Common phone number formats.
# US formats
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

Matches: (555) 123-4567, 555-123-4567, 555.123.4567, 5551234567

IP address (IPv4) patterns
Match IPv4 addresses.
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Matches: 192.168.1.1, 10.0.0.255 Note: This doesn't validate ranges (0-255). For strict validation, use a more complex pattern or validate in code.
Date (YYYY-MM-DD) patterns
Match ISO date format.
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Matches: 2026-03-14, 2025-12-31

Quick Reference Table

PatternMeaningExample
.Any characterh.t β†’ hat, hot
\d \w \sDigit, word, space\d+ β†’ 123
[abc]Character set[aeiou] β†’ vowels
[^abc]Negated set[^0-9] β†’ non-digit
* + ?0+, 1+, 0 or 1go+d β†’ god, good
{n,m}n to m times\d{2,4} β†’ 12, 1234
^ $Start, end^hello$
\bWord boundary\bcat\b
(abc)Capture group(\d+)px
(?:abc)Non-capturing`(?:a
a|bAlternationcat|dog
(?=...)Lookahead\d+(?=px)
(?<=...)Lookbehind(?<=\$)\d+

Try these patterns live β†’ Regex Tester