Regex Cheat Sheet: Complete Regular Expression Reference
A comprehensive reference guide to regular expression syntax, patterns, and examples for developers.
Introduction to Regular Expressions
Regular expressions (regex or regexp) are one of the most powerful tools in a developer's toolkit. They provide a concise and flexible way to search, match, and manipulate text based on patterns. Whether you're validating user input, parsing log files, extracting data from strings, or performing complex find-and-replace operations, regex is the go-to solution.
Regex was first formalized by mathematician Stephen Kleene in the 1950s as part of formal language theory. Today, regex is implemented in virtually every programming language (JavaScript, Python, Java, C#, Go, Rust, PHP, Ruby) and is built into tools like grep, sed, awk, and most text editors.
While regex can seem intimidating at first, understanding the core concepts makes it much more approachable. This cheat sheet covers everything from basic character matching to advanced lookaheads and backreferences.
Basic Character Matching
| Pattern | Description | Example |
|---|---|---|
| . | Any character except newline | h.t matches hat, hot, hit |
| \d | Any digit (0-9) | \d+ matches 123, 42 |
| \D | Any non-digit | \D+ matches abc, hello |
| \w | Word character (a-z, A-Z, 0-9, _) | \w+ matches hello_world |
| \W | Non-word character | \W matches @, #, spaces |
| \s | Whitespace (space, tab, newline) | \s+ matches spaces/tabs |
| \S | Non-whitespace | \S+ matches any_word |
| \b | Word boundary | \bcat\b matches cat not cats |
Quantifiers
| Pattern | Description | Example |
|---|---|---|
| * | Zero or more | ab*c matches ac, abc, abbc |
| + | One or more | ab+c matches abc, abbc (not ac) |
| ? | Zero or one (optional) | colou?r matches color, colour |
| {n} | Exactly n times | \d{3} matches 123 |
| {n,} | n or more times | \d{2,} matches 12, 123, 1234 |
| {n,m} | Between n and m times | \d{2,4} matches 12, 123, 1234 |
Character Classes & Groups
| Pattern | Description | Example |
|---|---|---|
| [abc] | Any of a, b, or c | [aeiou] matches vowels |
| [^abc] | Not a, b, or c | [^0-9] matches non-digits |
| [a-z] | Range: a through z | [a-zA-Z] matches any letter |
| (abc) | Capture group | (\d+)-(\d+) captures both numbers |
| (?:abc) | Non-capturing group | (?:http|https):// groups without capturing |
| a|b | Alternation (or) | cat|dog matches cat or dog |
Anchors & Boundaries
| Pattern | Description |
|---|---|
| ^ | Start of string (or line with m flag) |
| $ | End of string (or line with m flag) |
| \b | Word boundary |
| \B | Non-word boundary |
Lookaheads & Lookbehinds
Lookaheads and lookbehinds (collectively called lookarounds) are zero-width assertions that match a position without consuming characters:
| Pattern | Description | Example |
|---|---|---|
| (?=...) | Positive lookahead | \d+(?=px) matches 10 in 10px |
| (?!...) | Negative lookahead | \d+(?!px) matches 10 in 10em |
| (?<=...) | Positive lookbehind | (?<=\$)\d+ matches 50 in $50 |
| (?<!...) | Negative lookbehind | (?<!\$)\d+ matches 50 in €50 |
Common Regex Patterns
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}https?://[^\s/$.?#].[^\s]*\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b(?:\d{1,3}\.){3}\d{1,3}\b\d{4}-\d{2}-\d{2}#(?:[0-9a-fA-F]{3}){1,2}\b(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}<[^>]+>Regex Flags
| Flag | Name | Description |
|---|---|---|
| g | Global | Find all matches, not just the first |
| i | Case-insensitive | Match regardless of case |
| m | Multiline | ^ and $ match line starts/ends |
| s | DotAll | . matches newline characters too |
| u | Unicode | Enable full Unicode matching |
Test Your Regex Patterns
Try our free regex tester with real-time match highlighting, capture group display, and a built-in pattern library.
Open Regex Tester →