Regex Cheatsheet
Every regex syntax element with examples, plus common patterns.
By Λ · Updated May 18, 2026
Test patterns interactively in the regex tester. For the conceptual walkthrough, see my regex guide post.
Character Classes
| Pattern | Matches | Example |
|---|---|---|
| . | Any character except newline | a.c matches "abc", "aXc" |
| \d | Any digit (0-9) | \d{3} matches "123" |
| \D | Any non-digit | \D+ matches "abc!" |
| \w | Word character [A-Za-z0-9_] | \w+ matches "hello_123" |
| \W | Non-word character | \W matches space, ! etc. |
| \s | Whitespace (space, tab, newline) | \s+ matches " " |
| \S | Non-whitespace | everything but whitespace |
| [abc] | Any of a, b, or c | [aeiou] matches a vowel |
| [^abc] | Any except a, b, c | [^0-9] matches non-digit |
| [a-z] | Range a through z | [A-Za-z0-9] alphanumeric |
Quantifiers
| Pattern | Matches | Notes |
|---|---|---|
| * | 0 or more | Greedy by default |
| + | 1 or more | Greedy |
| ? | 0 or 1 (optional) | Or makes the previous quantifier lazy |
| {n} | Exactly n | \d{3} exactly 3 digits |
| {n,} | n or more | \d{3,} 3+ digits |
| {n,m} | Between n and m | \d{3,5} 3 to 5 digits |
| *? | Lazy: 0 or more, as few as possible | Use to avoid backtracking |
| +? | Lazy: 1 or more |
Anchors
| Pattern | Matches | Example |
|---|---|---|
| ^ | Start of string (or line in multiline mode) | ^hello |
| $ | End of string (or line) | world$ |
| \b | Word boundary | \bcat\b matches "cat" not "category" |
| \B | Non-word-boundary | \Bcat matches "scattered" inner cat |
Groups and Captures
| Pattern | Matches | Example |
|---|---|---|
| (abc) | Capturing group | (\d{3})-(\d{4}) captures area + number |
| (?:abc) | Non-capturing group | For grouping without saving |
| (?<name>abc) | Named capturing group | Refer back as \k<name> |
| \1, \2, \3 | Backreference to capture group | (a)\1 matches "aa" |
| | | Alternation (OR) | cat|dog matches either |
Lookarounds
| Pattern | Matches | Example |
|---|---|---|
| (?=abc) | Positive lookahead: followed by abc | \d+(?=px) match digits before "px" |
| (?!abc) | Negative lookahead: NOT followed by abc | \d+(?!px) |
| (?<=abc) | Positive lookbehind: preceded by abc | (?<=\$)\d+ match digits after $ |
| (?<!abc) | Negative lookbehind: NOT preceded by abc |
Flags / Modifiers
| Flag | Effect |
|---|---|
| i | Case-insensitive |
| g | Global (find all, not just first) |
| m | Multiline (^ and $ match line boundaries) |
| s | Dotall (. also matches newline) |
| u | Unicode mode |
| x | Extended (ignore whitespace in pattern, allow comments) [some engines] |
Common Patterns
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Email (RFC-incomplete but works for 99% of real addresses)
^https?:\/\/[^\s/$.?#].[^\s]*$URL (HTTP/HTTPS)
^\+?[1-9]\d{1,14}$Phone number (E.164 international format)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Date YYYY-MM-DD
^([01]?\d|2[0-3]):[0-5]\d(:[0-5]\d)?$Time HH:MM or HH:MM:SS (24-hour)
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$Hex color (#abc or #aabbcc)
^(?:[a-zA-Z0-9+\/]{4})*(?:[a-zA-Z0-9+\/]{2}==|[a-zA-Z0-9+\/]{3}=)?$Base64
^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$IPv4 (loose; for strict use the subnet calc)
^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$UUID
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)IPv4 (strict, validates each octet 0-255)
(?<=\$)\d+(\.\d{2})?US dollar amount preceded by $
^\d{3}-\d{2}-\d{4}$US Social Security Number format
\b[A-Z][A-Za-z0-9_]*\bClass name (PascalCase identifier)
\b[a-z][a-zA-Z0-9]*\bcamelCase identifier
^[a-z0-9]+(-[a-z0-9]+)*$Slug (lowercase, hyphenated)
Common Gotchas
- Greedy by default.
.*matches as much as possible. Use.*?for lazy when matching between delimiters. - Escape special characters.
. * + ? ( ) [ ] { } | \ ^ $need backslash to match literally. - Inside
[ ], fewer characters are special. Just] - ^ \need escaping inside a character class. - Anchors and multiline mode. Without
mflag,^and$match start/end of string, not lines. - Catastrophic backtracking. Patterns like
(a+)+bon long input can hang. Use atomic groups or avoid nested quantifiers. - JavaScript does not support possessive quantifiers.
a++is invalid. Use(?=(a+))\1as a workaround. - Lookbehind support varies. Modern JavaScript, Python, Java, .NET have it. Old engines do not.
Last updated