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
| Token | Meaning | Example | Matches |
|---|---|---|---|
. | Any character except newline | c.t | cat, c9t, c t |
\d | Digit 0–9 | \d\d | 42 |
\D | Not a digit | \D+ | abc |
\w | Word character: A–Z a–z 0–9 _ | \w+ | user_1 |
\W | Not a word character | \W | @ in a@b |
\s | Whitespace (space, tab, newline…) | a\sb | a b |
\S | Not whitespace | \S+ | word |
[abc] | One of a, b or c | gr[ae]y | gray, 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
Quantifiers
| Token | Meaning | Example | Matches |
|---|---|---|---|
* | 0 or more (greedy) | ab* | a, ab, abbb |
+ | 1 or more | ab+ | ab, abbb |
? | 0 or 1 (optional) | colou?r | color, 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
| Token | Meaning | Example | Matches |
|---|---|---|---|
(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 |
\1 | Backreference to group 1 | (\w)\1 | ll in hello |
\k<name> | Named backreference | (?<q>['"]).*?\k<q> | "hi" |
a|b | Alternation: a or b | cat|dog | cat, dog |
Lookarounds
| Token | Meaning | Example | Matches |
|---|---|---|---|
(?=…) | Followed by (lookahead) | \d+(?=px) | 12 in 12px |
(?!…) | Not followed by | \d+(?!px)\b | 12 in 12em |
(?<=…) | Preceded by (lookbehind) | (?<=\$)\d+ | 5 in $5 |
(?<!…) | Not preceded by | (?<!\$)\b\d+ | 5 in €5 |
Escapes
Flags
| Token | Meaning | Example | Matches |
|---|---|---|---|
g | Global: all matches | /a/g | every a |
i | Case-insensitive | /abc/i | ABC |
m | Multiline: ^ $ per line | /^\d/m | each line start |
s | dotAll: . matches newline | /a.b/s | a\nb |
u | Unicode mode | /\p{Emoji}/u | 🎉 |
y | Sticky: match at lastIndex only | /foo/y | tokenizers |
x | Verbose: ignore whitespace, allow # comments (Python re.X, PCRE) | (?x) \d+ # digits | 123 |
Replacement tokens
| Token | Meaning | Example | Matches |
|---|---|---|---|
$1 $2 | Group number (Python: \1 or \g<1>) | (\w+) (\w+) → $2 $1 | swap 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.