Regex Cheat Sheet

Free regular expressions cheat sheet: character classes, anchors, quantifiers, groups, lookarounds and common patterns, with examples.

Character Classes

ConceptSyntaxExample
Any character
Matches any single character except a newline.
.a.c matches "abc", "axc"
Digit
Any digit 0-9. \D is any non-digit.
\d\d matches "7" in "a7b"
Word character
[A-Za-z0-9_]. \W is the inverse.
\w\w matches letters, digits, underscore
Whitespace
\S matches any non-whitespace character.
\s\s matches a space, tab, or newline
Custom set / range
[^...] negates the set: [^0-9] is any non-digit.
[abc] / [a-z][aeiou] matches any vowel

Anchors

ConceptSyntaxExample
Start of string/line
With multiline mode, anchors the start of each line.
^^Hello matches "Hello" at the start
End of string/line
Matches the end of the string (or line in multiline mode).
$world$ matches "world" at the end
Word boundary
The position between a word and a non-word character.
\b\bcat\b matches "cat" but not "category"
Non-word boundary
The opposite of \b — not at a word boundary.
\B\Bcat matches "cat" inside "bobcat"
Start / end of input
Always the absolute start/end, ignoring multiline mode.
\A / \z\Astart anchors the whole string

Quantifiers

ConceptSyntaxExample
Zero or more
Matches the preceding token any number of times.
*ab*c matches "ac", "abc", "abbbc"
One or more
Requires at least one of the preceding token.
+\d+ matches "123"
Optional
Matches zero or one of the preceding token.
?colou?r matches "color" and "colour"
Exact / range count
{2,} is two or more; {2,4} is two to four.
{n} / {n,m}\d{3} matches exactly 3 digits
Lazy quantifier
Adds ? to match as few characters as possible.
*? / +?<.+?> matches the shortest tag

Groups & Alternation

ConceptSyntaxExample
Capturing group
Groups a pattern and captures the match for later use.
(...)(\d{4})-(\d{2}) captures year and month
Alternation
Matches the expression on either side of the |.
a|bcat|dog matches "cat" or "dog"
Non-capturing group
Groups for quantifiers without storing the match.
(?:...)(?:abc)+ groups without capturing
Named group
Capture by name instead of by position number.
(?<name>...)(?<year>\d{4})
Backreference
Re-matches text captured by an earlier group.
\1 / \k<name>(\w)\1 matches doubled letters like "ll"

Lookarounds

ConceptSyntaxExample
Positive lookahead
Asserts what follows without including it in the match.
(?=...)\d+(?= dollars) matches digits before " dollars"
Negative lookahead
Asserts that the following text does not match.
(?!...)\d+(?! dollars) digits NOT followed by " dollars"
Positive lookbehind
Asserts what precedes without including it.
(?<=...)(?<=\$)\d+ matches digits after a "$"
Negative lookbehind
Asserts that the preceding text does not match.
(?<!...)(?<!\$)\d+ digits NOT preceded by "$"
Why use them
Lookarounds check context but consume no characters.
assertions match position(?=.*\d) requires a digit somewhere ahead

Common Patterns

ConceptSyntaxExample
Email (simple)
A practical email matcher; full RFC validation is far more complex.
[\w.]+@[\w.]+\.\w+matches "ada@mail.com"
URL
s? makes the s optional so both http and https match.
https?://[^\s]+matches "https://example.com/page"
Digits only
Anchored start to end so the whole string must be digits.
^\d+$matches "12345" but not "12a"
Whitespace trim
Replace matches with "" to trim a string.
^\s+|\s+$matches leading/trailing spaces
Hex color
A hash followed by exactly six hex digits.
#[0-9a-fA-F]{6}matches "#1a2b3c"