Regular expressions (regex) are one of the most powerful text processing tools available to developers — and one of the most frequently forgotten. This cheat sheet covers the syntax you need for everyday pattern matching, from basic metacharacters to advanced lookahead assertions.
| Pattern | Matches | Example |
|---|
. | Any character except newline | a.c → "abc", "a1c" |
\d | Any digit (0-9) | \d{3} → "123" |
\D | Any non-digit | \D+ → "abc" |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ → "hello_world" |
\W | Non-word character | \W → "@", " " |
\s | Whitespace (space, tab, newline) | \s+ → " " |
\S | Non-whitespace | \S+ → "hello" |
\b | Word boundary | \bcat\b → "cat" but not "catch" |
| Pattern | Meaning |
|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{n} | Exactly n |
{n,} | n or more |
{n,m} | Between n and m |
*? | 0 or more (lazy/non-greedy) |
+? | 1 or more (lazy/non-greedy) |
Greedy vs lazy: By default, quantifiers are greedy — they match as much as possible. Adding ? makes them lazy — matching as little as possible.
"<b>bold</b>" with <.*> → matches "<b>bold</b>" (greedy)
"<b>bold</b>" with <.*?> → matches "<b>" (lazy)
| Pattern | Matches |
|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
| Pattern | Matches |
|---|
[abc] | Any of a, b, or c |
[^abc] | Any character except a, b, c |
[a-z] | Any lowercase letter |
[A-Z] | Any uppercase letter |
[0-9] | Any digit (same as \d) |
[a-zA-Z0-9_] | Same as \w |
| Pattern | Purpose |
|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
a|b | Alternation (a or b) |
\1 | Backreference to group 1 |
| Pattern | Name | Matches |
|---|
(?=abc) | Positive lookahead | Followed by "abc" |
(?!abc) | Negative lookahead | NOT followed by "abc" |
(?<=abc) | Positive lookbehind | Preceded by "abc" |
(?<!abc) | Negative lookbehind | NOT preceded by "abc" |
Lookarounds are zero-width assertions — they check a condition without consuming characters.
\d+(?= dollars) → "100" in "100 dollars"
(?<=\$)\d+ → "50" in "$50"
\b\w+(?!ing\b) → words not ending in "ing"
| Flag | Name | Effect |
|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | a matches A |
m | Multiline | ^ and $ match line boundaries |
s | Dotall | . matches newline characters |
u | Unicode | Full Unicode support |
[\w.-]+@[\w.-]+\.\w{2,}
https?:\/\/[\w.-]+(?:\/[\w./?#&=-]*)?
\b(?:\d{1,3}\.){3}\d{1,3}\b
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
(?:\+1[-.\s]?)?(?:\(?\d{3}\)?[-.\s]?)?\d{3}[-.\s]?\d{4}
#(?:[0-9a-fA-F]{3}){1,2}\b
<\/?[\w]+(?:\s+[\w-]+(?:="[^"]*")?)*\s*\/?>
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
- Not escaping special characters —
., *, +, ?, (, ), [, {, \, ^, $, | all have special meaning. Escape them with \ when matching literally. - Greedy matching —
.* in the middle of a pattern often matches more than expected. Use .*? for lazy matching or be more specific. - Not anchoring — without
^ and $, a pattern can match anywhere in the string, not just the full string. - Catastrophic backtracking — nested quantifiers like
(a+)+b can cause exponential backtracking. Avoid patterns where the regex engine has too many ways to match.
Test your regex patterns with live highlighting using StackCache Regex Tester. See matches, capture groups, and named groups in real time — all locally in your browser.