Regex Cheat Sheet
47 regular expression tokens across 8 categories — every anchor, character class, quantifier, group, assertion, escape, and flag, each with an example. Copyable, printable, and grouped so you can find what you need without scrolling through prose.
Want the explanation rather than the reference? Read theregular expressions guide, or try a token in theregex tester.
Anchors
| Token | Meaning | Example | |
|---|---|---|---|
| ^ | Start of string (or line, with the m flag) | ^Hello | |
| $ | End of string (or line, with the m flag) | world$ | |
| \b | Word boundary | \bcat\b | |
| \B | Not a word boundary | \Bcat\B |
Character Classes
| Token | Meaning | Example | |
|---|---|---|---|
| . | Any character except line breaks | a.c | |
| \d | Any digit (0-9) | \d{3} | |
| \D | Any non-digit | \D+ | |
| \w | Any word character (letter, digit, underscore) | \w+ | |
| \W | Any non-word character | \W | |
| \s | Any whitespace character | \s+ | |
| \S | Any non-whitespace character | \S+ | |
| [abc] | Any of a, b, or c | [abc] | |
| [^abc] | Any character except a, b, or c | [^abc] | |
| [a-z] | Any character in the range a to z | [a-z]+ |
Quantifiers
| Token | Meaning | Example | |
|---|---|---|---|
| * | Zero or more (greedy) | ab* | |
| + | One or more (greedy) | ab+ | |
| ? | Zero or one (optional) | colou?r | |
| {n} | Exactly n times | \d{4} | |
| {n,} | n or more times | \d{2,} | |
| {n,m} | Between n and m times | \d{2,4} | |
| *? | Zero or more (lazy) | <.*?> | |
| +? | One or more (lazy) | ".+?" |
Groups & Backreferences
| Token | Meaning | Example | |
|---|---|---|---|
| (...) | Capturing group | (ab)+ | |
| (?:...) | Non-capturing group | (?:ab)+ | |
| (?<name>...) | Named capturing group | (?<year>\d{4}) | |
| \1 | Backreference to group 1 | (\w)\1 | |
| \k<name> | Backreference to a named group | (?<q>['"])\k<q> | |
| | | Alternation (OR) | cat|dog |
Lookaround Assertions
| Token | Meaning | Example | |
|---|---|---|---|
| (?=...) | Positive lookahead | \d+(?=px) | |
| (?!...) | Negative lookahead | \d+(?!px) | |
| (?<=...) | Positive lookbehind | (?<=\$)\d+ | |
| (?<!...) | Negative lookbehind | (?<!\$)\d+ |
Escaping
| Token | Meaning | Example | |
|---|---|---|---|
| \. | Literal dot | 3\.14 | |
| \\ | Literal backslash | C:\\\\Users | |
| \/ | Literal forward slash | https:\/\/ | |
| \( | Literal opening parenthesis | \(\d{3}\) | |
| \[ | Literal opening bracket | \[note\] |
Unicode
| Token | Meaning | Example | |
|---|---|---|---|
| \p{L} | Any Unicode letter (needs u flag) | \p{L}+ | |
| \p{N} | Any Unicode number (needs u flag) | \p{N}+ | |
| \p{Emoji} | Any emoji character (needs u/v flag) | \p{Emoji} | |
| \u{1F600} | Unicode code point escape (needs u flag) | \u{1F600} |
Flags
| Token | Meaning | Example | |
|---|---|---|---|
| g | Global — find all matches, not just the first | — | |
| i | Case-insensitive matching | — | |
| m | Multiline — ^ and $ match line boundaries | — | |
| s | Dot-all — . also matches newlines | — | |
| u | Unicode — enables full Unicode matching | — | |
| y | Sticky — matches only from lastIndex | — |
Regex Syntax Questions
What is the difference between * and + in regex?
Both are quantifiers applied to the item before them. The asterisk means "zero or more", so it can match nothing at all, while the plus means "one or more" and requires at least one occurrence. That is why /ab*c/ matches "ac" but /ab+c/ does not.
What does \b mean in a regular expression?
\b matches a word boundary — the zero-width position between a word character (a letter, digit, or underscore) and anything else, including the start or end of the string. It consumes no characters, which is why /\bcat\b/ matches "cat" in "the cat sat" but not inside "concatenate".
What is the difference between greedy and lazy quantifiers?
A greedy quantifier such as .* takes as much text as it can and then gives characters back until the rest of the pattern fits. A lazy quantifier, written by adding a question mark as in .*?, takes as little as possible and expands only when it has to. Greedy matching is the default and is the usual cause of a pattern matching far more text than intended.
How do I escape a special character in regex?
Put a backslash in front of it. The characters that need escaping outside a character class are . ? * + ( ) [ ] { } | ^ $ / and the backslash itself. Inside a character class most of these are already literal — only ^ (at the start), ] , - , and \ need care.
Can I print this regex cheat sheet?
Yes. Use the print button at the top of the page — the layout is styled for print, so the tables fit cleanly on paper without the navigation, sidebar, or footer. It also works as a PDF via your browser's print dialog.
Keep Going
Regex Tester
Test a regular expression against sample text and see every match highlighted live.
Regex Generator
Build a pattern by clicking blocks — no syntax memorisation required.
Regular Expressions Guide
What regex is, how the syntax works, and the mistakes that trip people up.
Python Regex Guide
The re module end to end — search, match, findall, groups, and substitution.
Regex Pattern Library
100+ ready-to-use patterns for email, URLs, dates, IPs, and more.