Regex Cheat Sheet: The Ultimate Reference
Regular Expressions (Regex) are incredibly powerful but famously difficult to master. Whether you're validating an email address, scraping data, or searching through code, having a reliable Regex Cheat Sheet can save you hours of frustration.
In this guide, we break down every regex component, from basic characters to complex lookarounds, complete with visual explanations and practical examples.
1. Core Meta-characters
These are the building blocks of any regex pattern.
| Meta-character | Description | Example |
|---|---|---|
. |
Matches any single character except newline | a.b matches acb, a1b |
\ |
Escapes a special character | \. matches a literal . |
| |
Alternation (OR) | cat|dog matches cat or dog |
[] |
Character set (any character inside) | [aeiou] matches any vowel |
[^] |
Negated character set | [^0-9] matches any non-digit |
2. Quantifiers: Controlling Frequency
Quantifiers tell the regex engine how many times a character or group should appear.
| Quantifier | Description | Example |
|---|---|---|
* |
0 or more times | a* matches ``, a, aa |
+ |
1 or more times | a+ matches a, aa |
? |
0 or 1 time (optional) | a? matches `` or a |
{n} |
Exactly n times |
a{3} matches aaa |
{n,} |
n or more times |
a{2,} matches aa, aaa |
{n,m} |
Between n and m times |
a{2,4} matches aa, aaa, aaaa |
3. Character Classes: Common Shortcuts
| Class | Description | Equivalent |
|---|---|---|
\d |
Any digit | [0-9] |
\D |
Any non-digit | [^0-9] |
\w |
Any word character (alphanumeric + _) |
[a-zA-Z0-9_] |
\W |
Any non-word character | [^a-zA-Z0-9_] |
\s |
Any whitespace (space, tab, newline) | [ \t\n\r\f\v] |
\S |
Any non-whitespace | [^ \t\n\r\f\v] |
4. Anchors: Defining Boundaries
| Anchor | Description | Example |
|---|---|---|
^ |
Start of string / line | ^Hello |
$ |
End of string / line | World$ |
\b |
Word boundary | \bcat\b (matches "cat" but not "category") |
\B |
Non-word boundary | \Bcat (matches "category") |
5. Groups and Capturing
| Syntax | Description | Example |
|---|---|---|
(...) |
Capturing group | (abc)+ matches abcabc |
(?:...) |
Non-capturing group | (?:abc)+ |
(?<name>...) |
Named capturing group | (?<id>\d+) |
\1 |
Backreference to group #1 | (\w)\1 matches aa, bb |
6. Lookaround: Advanced Filtering
Lookarounds allow you to match a pattern only if it is (or isn't) followed or preceded by another pattern, without including that pattern in the match.
| Syntax | Name | Description |
|---|---|---|
(?=...) |
Positive Lookahead | Followed by ... |
(?!...) |
Negative Lookahead | NOT followed by ... |
(?<=...) |
Positive Lookbehind | Preceded by ... |
(?<!...) |
Negative Lookbehind | NOT preceded by ... |
7. Visualizing Regex with Railroad Diagrams
Complex regex patterns can quickly become unreadable. Railroad Diagrams are a visual way to represent the flow of a regular expression.
Imagine a train track:
- The track splits at an alternation (
|). - The track loops for a quantifier (
*,+). - The track passes through a group box.
Using a visualizer helps you debug logic errors, especially when dealing with nested groups or complex quantifiers.
8. Practical Examples
Email Validation (Simplified)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Strong Password (Lookahead Example)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
(Requires at least one lowercase, one uppercase, one digit, and 8+ characters)
Phone Number (US Format)
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
9. Frequently Asked Questions (FAQ)
Q: What is the difference between "Greedy" and "Lazy" matching?
A: By default, quantifiers are greedy—they match as much as possible. Adding a ? after a quantifier (e.g., .*?) makes it lazy (or non-greedy), meaning it matches as little as possible.
Q: How do I perform a case-insensitive search?
A: Most regex engines use a flag (usually i) to enable case-insensitivity. For example, /hello/i matches Hello, HELLO, and hello.
Q: What is backtracking and why does it matter?
A: Backtracking occurs when the regex engine tries one path, fails, and "backtracks" to try another. Complex patterns with many nested quantifiers can lead to Catastrophic Backtracking, which can freeze your application or server.
Test Your Patterns on Tool3M
- Regex Tester: Real-time regex testing with highlighting and visual debugging.
- JSON Formatter: Use regex to search and filter your large JSON datasets.
- Case Converter: Clean up your text before applying regex filters.