Regex Cheat Sheet — 20 Common Patterns with Examples You Can Test
Last updated: April 20268 min readDeveloper Tools
Twenty regex patterns you will actually use. Each one includes the pattern, what it matches, and a test example. Copy any pattern and test it instantly against your own data.
Text & Validation Patterns
| # | Pattern Name | Regex | Example Match |
|---|
| 1 | Email address | [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,} | [email protected] |
| 2 | URL (http/https) | https?://[^\\s/$.?#][^\\s]* | https://example.com/page?q=1 |
| 3 | IPv4 address | \\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b | 192.168.1.1 |
| 4 | US phone number | \\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4} | (555) 123-4567 |
| 5 | Date (YYYY-MM-DD) | \\d{4}-\\d{2}-\\d{2} | 2026-04-01 |
| 6 | US zip code | \\d{5}(-\\d{4})? | 90210, 90210-1234 |
| 7 | Hex color code | #[0-9a-fA-F]{3,8} | #FF5733, #fff, #00aaff80 |
Data Extraction Patterns
| # | Pattern Name | Regex | Example Match |
|---|
| 8 | HTML tags | <[^>]+> | <div class="box">, <br/> |
| 9 | Username (alphanumeric) | ^[a-zA-Z0-9_]{3,20}$ | cool_user_42 |
| 10 | Password strength (8+ chars, upper, lower, digit) | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$ | MyPass123 |
| 11 | Credit card (basic 16 digits) | \\b\\d{4}[- ]?\\d{4}[- ]?\\d{4}[- ]?\\d{4}\\b | 4111-1111-1111-1111 |
| 12 | File extension | \\.([a-zA-Z0-9]{1,10})$ | .pdf, .jpg, .html |
| 13 | Trim whitespace | ^\\s+|\\s+$ | Leading/trailing spaces |
| 14 | Duplicate consecutive words | \\b(\\w+)\\s+\\1\\b | the the, is is |
Developer & Content Patterns
| # | Pattern Name | Regex | Example Match |
|---|
| 15 | Markdown link | \\[([^\\]]+)\\]\\(([^)]+)\\) | [text](https://url.com) |
| 16 | CSV value (quoted) | "([^"]*)" | "John Smith","New York" |
| 17 | JSON key | "([^"]+)"\\s*: | "name": "value" |
| 18 | URL slug | ^[a-z0-9]+(-[a-z0-9]+)*$ | my-blog-post-title |
| 19 | Emoji (Unicode range) | [\\u{1F600}-\\u{1F64F}] | Matches face emojis |
| 20 | Hashtag | #[a-zA-Z0-9_]+ | #regex, #coding, #dev_tools |
How to Use This Cheat Sheet
- Find the pattern you need from the tables above
- Copy the regex pattern
- Open Regex Tester
- Paste the pattern and your test data
- Verify it matches what you expect (and does not match what it should not)
- Copy the working pattern into your code
Always test against edge cases, not just the happy path. An email regex that matches "[email protected]" might also match "[email protected]" if you are not careful.
Quick Syntax Reference
| Symbol | Meaning | Example |
|---|
| \\d | Any digit (0-9) | \\d{3} matches 123 |
| \\w | Word character (letter, digit, underscore) | \\w+ matches hello_42 |
| \\s | Whitespace (space, tab, newline) | \\s+ matches any whitespace run |
| . | Any character (except newline without s flag) | a.c matches abc, a1c, a-c |
| + | One or more of previous | \\d+ matches 1, 42, 999 |
| * | Zero or more of previous | \\d* matches empty string or digits |
| ? | Zero or one of previous (also: lazy modifier) | colou?r matches color and colour |
| {n,m} | Between n and m repetitions | \\d{2,4} matches 42, 123, 1234 |
| ^ | Start of string (or line with m flag) | ^ matches start of string |
| $ | End of string (or line with m flag) | $ matches end of string |
| [abc] | Character class: a, b, or c | [aeiou] matches any vowel |
| [^abc] | Negated class: NOT a, b, or c | [^0-9] matches non-digits |
| (abc) | Capture group | (\\d+)-(\\d+) captures both numbers |
| a|b | Alternation: a or b | cat|dog matches either word |
| \\b | Word boundary | \\bcat\\b matches cat but not catalog |
Related Developer Tools