Regular expressions are one of the most powerful tools in a developer's toolkit — and one of the most confusing. This cheat sheet covers the patterns you'll actually use day to day.
Basic Matching
| Pattern | Matches | Example |
|---|
. | Any character except newline | h.t matches "hat", "hot" |
\d | Any digit (0-9) | \d\d matches "42" |
\w | Word character (letter, digit, _) | \w+ matches "hello_42" |
\s | Whitespace (space, tab, newline) | hello\sworld |
\b | Word boundary | \bcat\b matches "cat" but not "catch" |
Quantifiers
| Pattern | Meaning |
|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{3,} | 3 or more |
Character Classes
| Pattern | Matches |
|---|
[abc] | Any of a, b, or c |
[^abc] | Any character NOT a, b, or c |
[a-z] | Any lowercase letter |
[0-9] | Any digit (same as \d) |
[a-zA-Z0-9_] | Any word character (same as \w) |
Anchors & Groups
| Pattern | Meaning |
|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
(abc) | Capture group |
(?:abc) | Non-capturing group |
a|b | Alternation (a or b) |
Real-World Patterns
Here are patterns you'll actually copy-paste:
| Purpose | Pattern |
|---|
| Email (simple) | [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} |
| URL | https?://[^\s]+ |
| IP Address | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} |
| Hex Color | #[0-9a-fA-F]{3,8} |
| Phone (US) | \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
Flags
g — Global: find all matches, not just the firsti — Case-insensitive: A matches am — Multiline: ^ and $ match line boundariess — Dotall: . matches newlines too
Test Your Regex
Try these patterns in our Regex Tester — it shows live matches, capture groups, and highlighted results as you type.