Developer Reference
Regex Cheatsheet — Complete Reference
Regex is the single most portable pattern-matching syntax in programming — the same expressions work in JavaScript, Python, grep, and every code editor. This cheatsheet covers everything you'll actually use, grouped by category.
Character classes
| Token | Matches |
|---|---|
| . | Any character except newline |
| \d | Digit 0–9 |
| \D | Non-digit |
| \w | Word char (letter, digit, _) |
| \W | Non-word char |
| \s | Whitespace (space, tab, newline) |
| \S | Non-whitespace |
| [abc] | Any of a, b, c |
| [^abc] | Anything except a, b, c |
| [a-z] | Range a through z |
Quantifiers
| Token | Matches |
|---|---|
| * | 0 or more |
| + | 1 or more |
| ? | 0 or 1 |
| {n} | Exactly n |
| {n,} | n or more |
| {n,m} | Between n and m |
| *? | 0 or more (lazy) |
| +? | 1 or more (lazy) |
Anchors & boundaries
| Token | Matches |
|---|---|
| ^ | Start of string (or line in multiline) |
| $ | End of string (or line in multiline) |
| \b | Word boundary |
| \B | Non-word-boundary |
Groups & lookaround
| Token | Matches |
|---|---|
| (abc) | Capturing group |
| (?:abc) | Non-capturing group |
| (?<name>abc) | Named group |
| (?=abc) | Positive lookahead |
| (?!abc) | Negative lookahead |
| (?<=abc) | Positive lookbehind |
| (?<!abc) | Negative lookbehind |
| \1 | Backreference to group 1 |
Common patterns
Email (rough)
^\S+@\S+\.\S+$URL
https?:\/\/[^\s]+IPv4
\b(?:\d{1,3}\.){3}\d{1,3}\bHex color
#(?:[0-9a-fA-F]{3}){1,2}\bISO date
\d{4}-\d{2}-\d{2}Phone (E.164)
\+?[1-9]\d{1,14}Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$Username (3–20 alnum/_/-)
^[A-Za-z0-9_-]{3,20}$Common pitfalls
- Greedy vs lazy:
.*in HTML often grabs way more than you want. Use.*?or a negated character class. - Backtracking bombs: nested quantifiers like
(a+)+can hang on adversarial input. - Escape special chars:
. + * ? ^ $ ( ) [ ] { } | \\must be escaped in literal patterns. - Multiline flag:
^and$match line boundaries only with themflag.
Try it on Toolzer
- Regex Tester — test patterns with live match highlighting.
- Find & Replace — bulk regex substitutions on any text.
- Email Extractor — pull all email addresses from text.
- Link Extractor — pull all URLs from a page.
Frequently asked questions
Which regex flavor should I learn?+
PCRE (Perl-Compatible Regular Expressions) is the closest to a lingua franca — JavaScript, Python, PHP, Ruby, and grep all support most of PCRE. Learn PCRE first, then pick up your language's quirks.
Why is my regex slow?+
Usually catastrophic backtracking. Patterns like (a+)+ or nested quantifiers can take exponential time on non-matching input. Prefer atomic groups (?>...) and possessive quantifiers when available.
How do I match a URL?+
Don't try to handle every edge case with a regex — URL parsing has 40+ RFCs. Use a URL parser (URL constructor in JS, urllib in Python). Regex is fine for a rough extraction: /https?:\/\/\S+/.
What's the difference between .* and .*?+
The first is greedy — it matches as much as possible. The second (with ?) is lazy — it matches as little as possible. Use lazy quantifiers when scanning HTML/JSON to avoid grabbing too much.
Should I validate emails with regex?+
For UI hints, yes — a simple /^\S+@\S+\.\S+$/ catches typos. For real validation, send a confirmation email. The full RFC 5322 email regex is 6,000+ characters and still not comprehensive.
