Regex Cheat Sheet: Common Patterns Every Developer Should Know
Regular expressions are one of those skills that feel cryptic until they suddenly click — and then you wonder how you ever lived without them. A regex is a compact pattern for matching, searching, and replacing text, and the same core syntax works across JavaScript, Python, Java, PHP, Go, and almost every text editor and command-line tool. This cheat sheet covers the pieces you actually use day to day, with short examples you can test as you read.
Tip: keep the Regex Tester open in another tab and paste each pattern as you go. Seeing matches highlight live is the fastest way to build intuition.
Character classes
Character classes match "one character of a certain kind."
| Pattern | Matches |
|---|---|
. | Any character except a newline |
\d | Any digit (0–9) |
\D | Any non-digit |
\w | Word character (letters, digits, underscore) |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | Any one of a, b, or c |
[^abc] | Any character except a, b, or c |
[a-z] | Any lowercase letter in the range |
Quantifiers
Quantifiers say "how many" of the preceding item to match.
| Pattern | Meaning |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | 2 or more |
By default quantifiers are greedy — they match as much as possible. Add a ?
to make them lazy (match as little as possible), which matters a lot when parsing tags
or quotes:
Greedy: <.+> on "<a><b>" matches "<a><b>" Lazy: <.+?> on "<a><b>" matches "<a>" then "<b>"
Anchors and boundaries
Anchors do not match characters; they match positions.
| Pattern | Matches at |
|---|---|
^ | Start of the string (or line in multiline mode) |
$ | End of the string (or line) |
\b | A word boundary |
\B | Not a word boundary |
For example, \bcat\b matches "cat" as a whole word but not the "cat" inside "category" or
"scatter."
Groups and alternation
| Pattern | Meaning |
|---|---|
(abc) | Capturing group — remembers the match |
(?:abc) | Non-capturing group — groups without remembering |
(?<name>abc) | Named capturing group |
a|b | Match a or b (alternation) |
\1 | Backreference to the first captured group |
Groups are what make find-and-replace powerful. With capturing groups you can reorder a date from
YYYY-MM-DD to DD/MM/YYYY in a single replace operation using
$1, $2, and $3 in the replacement string.
Flags
| Flag | Effect |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive |
m | Multiline — ^ and $ match each line |
s | Dot matches newlines too |
Ready-to-use patterns
These are practical starting points. Adapt them to your needs and always test against real data — there is no single "perfect" pattern for messy real-world input.
Email (practical, not RFC-perfect)
^[\w.+-]+@[\w-]+\.[\w.-]+$
URL (http/https)
^https?:\/\/[^\s/$.?#].[^\s]*$
Date (YYYY-MM-DD)
^\d{4}-\d{2}-\d{2}$
Time (24-hour HH:MM)
^([01]\d|2[0-3]):[0-5]\d$
Hex color
^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Strong-ish password (8+ chars, upper, lower, digit)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
That last one uses lookahead: (?=...) checks that a condition holds at the
current position without consuming characters. Three lookaheads stacked together require a lowercase
letter, an uppercase letter, and a digit, while .{8,} enforces the length.
Three mistakes to avoid
- Forgetting to escape special characters. A literal dot is
\.; an unescaped.matches anything. - Using regex to parse HTML or JSON. These are nested structures — use a real parser instead. Regex is for flat, predictable text.
- Catastrophic backtracking. Patterns like
(a+)+on certain inputs can hang for seconds. Keep quantifiers simple and test with worst-case input.
Test and build your patterns
The fastest way to learn regex is to experiment with instant feedback. These tools help:
Conclusion
Regex rewards a small amount of memorization with a huge amount of power. Learn the character classes, quantifiers, anchors, and groups in this sheet and you can already handle the vast majority of real tasks: validation, search-and-replace, log parsing, and data cleanup. Keep a tester handy, build patterns incrementally, and reach for a proper parser when your input is genuinely nested.
Frequently Asked Questions
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers match as much text as possible; lazy quantifiers (adding ?) match as little as possible. This matters when a pattern could match multiple lengths.
What does \b mean in regex?
\b matches a word boundary — the position between a word character and a non-word character. It lets you match whole words.
Why should I not parse HTML with regex?
HTML is nested and irregular, which regex cannot reliably handle. Use a dedicated HTML parser instead; regex is best for flat, predictable text.
Are regex patterns the same in every language?
The core syntax is largely shared, but some features and flags differ between engines (for example, JavaScript, PCRE, and Python). Most patterns in this sheet work everywhere.
How do I test a regex quickly?
Paste your pattern and sample text into an online Regex Tester to see matches highlighted in real time.