Regex Cheat Sheet: Every Pattern You'll Ever Need
Regular expressions are the Swiss Army knife of text processing. They are used in every programming language, every text editor, every search tool. They are also famously difficult to remember.
This cheat sheet is designed to be the one reference you actually bookmark. Every section is organized by category with real-world examples you can copy directly into your code.
Anchors
Anchors do not match characters — they match positions. Use them to pin your pattern to the start or end of a string or line.
| Pattern | Description | Example |
|---|---|---|
| ^ | Start of string (or line with m flag) | ^Hello matches "Hello world" |
| $ | End of string (or line with m flag) | world$ matches "Hello world" |
| \b | Word boundary | \bcat\b matches "cat" but not "catalog" |
| \B | Not a word boundary | \Bcat\B matches "concatenate" |
Character Classes
Match specific sets of characters. These are the building blocks of most patterns.
| Pattern | Description |
|---|---|
| . | Any character except newline |
| \d | Digit [0-9] |
| \D | Not a digit [^0-9] |
| \w | Word character [a-zA-Z0-9_] |
| \W | Not a word character |
| \s | Whitespace (space, tab, newline) |
| \S | Not whitespace |
| [abc] | Any of a, b, or c |
| [^abc] | Not a, b, or c |
| [a-z] | Any lowercase letter |
| [0-9] | Any digit (same as \d) |
Quantifiers
Control how many times a character or group must appear. By default, quantifiers are greedy — they match as much as possible. Add ? to make them lazy (match as little as possible).
| Pattern | Description | Lazy version |
|---|---|---|
| * | 0 or more | *? |
| + | 1 or more | +? |
| ? | 0 or 1 (optional) | ?? |
| {3} | Exactly 3 | — |
| {2,5} | Between 2 and 5 | {2,5}? |
| {3,} | 3 or more | {3,}? |
Greedy vs. lazy matters. Given the string <b>bold</b>, the pattern <.*> (greedy) matches the entire string. The pattern <.*?> (lazy) matches just <b>.
Groups and Alternation
Capture parts of your match for extraction, or use alternation to match one of several options.
| Pattern | Description |
|---|---|
| (abc) | Capturing group — captures "abc" |
| (?:abc) | Non-capturing group — groups without capturing |
| (?<name>abc) | Named capturing group |
| a|b | Alternation — matches "a" or "b" |
| \1 | Backreference to group 1 |
| \k<name> | Backreference to named group |
Lookaheads and Lookbehinds
Match a position based on what comes before or after it, without consuming those characters. Incredibly powerful for complex validation.
| Pattern | Description | Example |
|---|---|---|
| (?=abc) | Positive lookahead — followed by "abc" | \d+(?=px) matches "12" in "12px" |
| (?!abc) | Negative lookahead — NOT followed by "abc" | \d+(?!px) matches "12" in "12em" |
| (?<=abc) | Positive lookbehind — preceded by "abc" | (?<=\$)\d+ matches "50" in "$50" |
| (?<!abc) | Negative lookbehind — NOT preceded by "abc" | (?<!\$)\d+ matches "50" in "50kg" |
Flags
| Flag | Description |
|---|---|
| g | Global — find all matches, not just the first |
| i | Case-insensitive |
| m | Multiline — ^ and $ match line boundaries |
| s | Dotall — . matches newlines too |
| u | Unicode — full Unicode support |
20 Real-World Patterns
Copy these directly into your code. Each one has been tested and handles edge cases properly.
| Use Case | Pattern |
|---|---|
| Email (simple) | ^[\w.+-]+@[\w-]+\.[\w.]+$ |
| URL | https?://[\w\-._~:/?#\[\]@!$&'()*+,;=%]+ |
| IPv4 address | \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b |
| Hex color | #([0-9a-fA-F]{3}){1,2}\b |
| US phone | \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) |
| Time (HH:MM) | (?:[01]\d|2[0-3]):[0-5]\d |
| US ZIP code | \d{5}(?:-\d{4})? |
| Username | ^[a-zA-Z][\w]{2,29}$ |
| Strong password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$ |
| Slug (URL-safe) | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| HTML tag | <([a-z]+)(\s[^>]*)?\s*/?> |
| CSS hex color | #(?:[0-9a-f]{3,4}){1,2}\b |
| Credit card (generic) | \b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b |
| SSN (US) | \b\d{3}-\d{2}-\d{4}\b |
| Float/decimal | -?\d+\.?\d* |
| Whitespace trim | ^\s+|\s+$ |
| Duplicate words | \b(\w+)\s+\1\b |
| Markdown bold | \*\*(.+?)\*\* |
| File extension | \.([a-zA-Z0-9]+)$ |
Tips for Writing Better Regex
- Start simple, then refine. Get a basic pattern working first, then add edge case handling.
- Use non-capturing groups
(?:...)when you do not need the match — it is slightly faster. - Be specific.
\d{4}-\d{2}-\d{2}is better than.+-.+-.+for matching dates. - Test with edge cases. Empty strings, special characters, Unicode, and very long inputs all behave differently.
- Comment complex patterns. Most regex engines support the
xflag for free-spacing mode with inline comments. - Avoid catastrophic backtracking. Nested quantifiers like
(a+)+can cause exponential time on non-matching inputs. Use atomic groups or possessive quantifiers when available.
Test Your Regex Patterns
Paste a pattern, test it against sample text, and see matches highlighted in real time.
Open Regex TesterGet Tool Updates
New tools and guides delivered to your inbox. No spam, unsubscribe anytime.
You're subscribed! We'll keep you posted.