A useful regex cheat sheet should tell you what a token does, where it differs between engines, and what mistake commonly follows. This reference covers the shared core first, then calls out constructs that require engine-specific verification.
Examples are written as bare patterns. When used in source code, they may require escaping or delimiters.
Literal and Special Characters
| Syntax | Meaning | Example | ||
|---|---|---|---|---|
abc |
Literal sequence | cat matches cat |
||
. |
Any character except line terminators by default | a.c matches abc |
||
. |
Literal dot | v1.2 |
||
\ |
Literal backslash at regex level | Engine/string escaping varies | ||
| `a | b` | Literal pipe | Matches `a | b` |
Regex metacharacters commonly include . ^ $ * + ? ( ) [ ] { } | . Escape them when they should be literal, but avoid unnecessary escaping because some engines reject unknown escape sequences.
Character Classes
| Syntax | Meaning |
|---|---|
[abc] |
One of a, b, or c |
[a-z] |
One character in the stated range |
[^abc] |
One character except a, b, or c |
d / D |
Digit / non-digit; Unicode rules vary |
w / W |
Word / non-word character; definition varies |
s / S |
Whitespace / non-whitespace |
p{L} |
Unicode letter in engines supporting property escapes |
Prefer [0-9] when a specification explicitly requires ASCII digits. For international names or identifiers, research the target engine's Unicode properties instead of expanding an English-only range.
Anchors and Boundaries
| Syntax | Meaning |
|---|---|
^ |
Start of input, or line in multiline mode |
$ |
End of input/line, with engine-specific end behavior |
b |
Word boundary based on the engine's word definition |
B |
Not a word boundary |
For strict validation, use the engine's whole-match API when one exists. Python's re.fullmatch, Java's Matcher.matches, and similar APIs reduce ambiguity around anchors.
Quantifiers
| Syntax | Meaning |
|---|---|
x* |
Zero or more x |
x+ |
One or more x |
x? |
Zero or one x |
x{3} |
Exactly three x |
x{2,5} |
Two through five x |
x{2,} |
At least two x |
x+? |
Lazy form in common backtracking engines |
Greedy quantifiers try to consume as much as possible while allowing the full pattern to succeed. Lazy quantifiers try less first. Neither automatically fixes a poorly bounded pattern.
Groups and Alternation
| Syntax | Meaning | |
|---|---|---|
(abc) |
Capturing group | |
(?:abc) |
Noncapturing group in many engines | |
| `a | b` | Alternation |
1 |
Numeric backreference; syntax/context varies | |
(?<name>abc) |
Named group in JavaScript and some engines | |
(?P<name>abc) |
Python named-group syntax |
Alternation has low precedence. ^cat|dog$ means “starts with cat OR ends with dog,” not “is exactly cat or dog.” Use:
^(?:cat|dog)$
Lookarounds
| Syntax | Meaning |
|---|---|
x(?=y) |
x followed by y |
x(?!y) |
x not followed by y |
(?<=y)x |
x preceded by y |
(?<!y)x |
x not preceded by y |
Lookarounds assert context without adding that context to the full match. Support and lookbehind length restrictions differ, so test them in the production engine.
Common Flags
| Flag | Common purpose |
|---|---|
i |
Case-insensitive matching |
m |
Multiline behavior for ^ and $ |
s |
Dot matches line terminators |
g |
Find all/global in JavaScript-style APIs |
u |
Unicode-aware mode in JavaScript |
x |
Free-spacing/verbose mode in supporting engines |
Flags are API-specific. JavaScript's g also makes regex objects stateful through lastIndex, which can surprise code that repeatedly calls test().
Practical Patterns
ASCII integer
^[+-]?[0-9]+$
Simple slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
ISO-shaped calendar date
^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$
This checks shape and broad ranges, not whether 2025-02-31 exists. Parse the date afterward.
Duplicate adjacent ASCII word
b([A-Za-z]+)s+1b
Enable case-insensitive matching if The the should count.
Text between double quotes without escaped quotes
"[^"]*"
If escape sequences are allowed, the grammar is more complex and must be adapted to the target format.
Replacement References
Replacement syntax is not portable. JavaScript commonly uses $1 and $<name>:
"Doe, Jane".replace(/^(?<last>[^,]+),s*(?<first>.+)$/, "$<first> $<last>");
Python re.sub supports forms such as g<1> and g<name>:
import re
result = re.sub(r"^(?P<last>[^,]+),s*(?P<first>.+)$", r"g<first> g<last>", "Doe, Jane")
Troubleshooting Checklist
- Confirm engine, runtime version, flags, and API.
- Check whether you are searching or validating.
- Inspect string-literal escaping separately from regex escaping.
- Test empty, shortest, longest, Unicode, multiline, and near-miss inputs.
- Replace broad dots with explicit delimiters where possible.
- Check every capture consumed by downstream code.
- Benchmark long failing strings if input is untrusted.
Best Practices
Keep the pattern close to executable examples. Prefer named groups for data extraction, noncapturing groups for structure, and a verbose mode for patterns that require explanation. Never copy a validation pattern without understanding what it deliberately accepts and rejects.
This regex cheat sheet is a navigation aid, not a language specification. When a feature affects correctness or security, follow the documentation for the exact engine and add automated tests.
FAQ
What does .* mean?
It means zero or more occurrences of almost any character; newline behavior depends on the engine and flags.
What is ?: inside a group?
In engines that support it, (?:...) groups tokens without creating a capture.
Why does b fail around Unicode text?
Word-boundary behavior depends on the engine's definition of word characters and its Unicode mode.
Are lookbehinds supported everywhere?
No. Availability and permitted pattern lengths vary by engine and version.
How do I match a literal question mark?
Use ? outside a character class.
Suggested Internal Links
- Regex Tutorial for Beginners
- Regex Groups and Backreferences
- Regex Lookahead and Lookbehind
- Regex Testing and Debugging