Regex Cheat Sheet
Basic Patterns
Section titled “Basic Patterns”| Pattern | Matches |
|---|---|
. | Any single character (except newline) |
\d | Digit (0-9) |
\D | Non-digit |
\w | Word character (a-z, A-Z, 0-9, _) |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
| | Newline | | \t | Tab | | | Carriage return |
Anchors
Section titled “Anchors”| Pattern | Matches |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
| Word boundary |
\B | Not a word boundary |
\A | Start of string (absolute) |
\Z | End of string (absolute) |
Quantifiers
Section titled “Quantifiers”| Pattern | Matches |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{3,} | 3 or more |
{3,5} | Between 3 and 5 |
*? | 0 or more (lazy/non-greedy) |
+? | 1 or more (lazy/non-greedy) |
?? | 0 or 1 (lazy/non-greedy) |
Greedy vs Lazy
Section titled “Greedy vs Lazy”String: <div>hello</div>
Greedy: <.*> matches "<div>hello</div>"Lazy: <.*?> matches "<div>"Character Classes
Section titled “Character Classes”| Pattern | Matches |
|---|---|
[abc] | a, b, or c |
[^abc] | Not a, b, or c |
[a-z] | Lowercase letter |
[A-Z] | Uppercase letter |
[0-9] | Digit (same as \d) |
[a-zA-Z] | Any letter |
[a-zA-Z0-9_] | Word character (same as \w) |
[\s\S] | Any character including newline |
Inside character classes:
- Most special characters are literal (no escaping needed)
- Escape:
],\,^(at start),-(in middle)
Groups & Capturing
Section titled “Groups & Capturing”| Pattern | Description |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1, \2 | Backreference to group 1, 2 |
(?P=name) | Backreference by name (Python) |
\k<name> | Backreference by name (JS, .NET) |
Examples
Section titled “Examples”# Capture area code from phone number\((\d{3})\) \d{3}-\d{4}Input: (415) 555-1234Group 1: 415
# Named group(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
# Backreference - match repeated words(\w+)\s+\1Matches: "the the", "is is"Alternation
Section titled “Alternation”| Pattern | Matches |
|---|---|
a|b | a or b |
(cat|dog) | cat or dog (captured) |
(?:cat|dog) | cat or dog (not captured) |
Lookahead & Lookbehind
Section titled “Lookahead & Lookbehind”| Pattern | Name | Description |
|---|---|---|
(?=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 |
Examples
Section titled “Examples”# Password: at least one digit, one uppercase, one lowercase^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$
# Match "foo" not followed by "bar"foo(?!bar)Matches: "foobaz", "foo "Skips: "foobar"
# Match number preceded by $(?<=\$)\d+Input: "Price: $100"Matches: "100"
# Match word not preceded by @(?<!@)wordFlags/Modifiers
Section titled “Flags/Modifiers”| Flag | Name | Description |
|---|---|---|
i | Case insensitive | A matches a |
g | Global | Find all matches |
m | Multiline | ^/$ match line start/end |
s | Dotall | . matches newline |
x | Extended | Ignore whitespace, allow comments |
u | Unicode | Full Unicode support |
Inline flags
Section titled “Inline flags”(?i)case insensitive(?i:just this part) rest is case sensitive(?-i)turn off case insensitiveCommon Patterns
Section titled “Common Patterns”Email (simplified)
Section titled “Email (simplified)”[\w.-]+@[\w.-]+\.\w{2,}https?://[^\s/$.?#].[^\s]*IPv4 Address
Section titled “IPv4 Address”\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}Date (YYYY-MM-DD)
Section titled “Date (YYYY-MM-DD)”\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])Phone (US)
Section titled “Phone (US)”(?:\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}HTML Tag
Section titled “HTML Tag”<([a-z]+)[^>]*>.*?</\1>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}^[a-z0-9]+(?:-[a-z0-9]+)*$Quoted String (with escapes)
Section titled “Quoted String (with escapes)”"(?:[^"\]|\.)*"Comments (C-style)
Section titled “Comments (C-style)”//.*|/\*[\s\S]*?\*/Language-Specific Notes
Section titled “Language-Specific Notes”JavaScript
Section titled “JavaScript”const re = /pattern/flags;const re = new RegExp('pattern', 'flags');
str.match(re) // Array of matches or nullstr.test(re) // Booleanstr.replace(re, 'new')str.split(re)re.exec(str) // Detailed match info
// Named groups (ES2018+)const match = /(?<year>\d{4})/.exec('2024');match.groups.year // "2024"Python
Section titled “Python”import re
re.search(pattern, string) # First matchre.match(pattern, string) # Match at start onlyre.findall(pattern, string) # All matches as listre.finditer(pattern, string) # Iterator of match objectsre.sub(pattern, repl, string) # Replacere.split(pattern, string) # Split
# Compile for reuseregex = re.compile(r'pattern', re.IGNORECASE)regex.search(string)
# Named groupsmatch = re.search(r'(?P<year>\d{4})', '2024')match.group('year') # "2024"grep/ripgrep
Section titled “grep/ripgrep”grep -E 'pattern' file # Extended regexgrep -P 'pattern' file # Perl regex (GNU grep)grep -o 'pattern' file # Only matching partgrep -i 'pattern' file # Case insensitivegrep -v 'pattern' file # Invert match
rg 'pattern' file # Rust regex (fast)rg -i 'pattern' # Case insensitiverg -o 'pattern' # Only matching partrg --pcre2 'pattern' # Perl-compatible regexsed 's/pattern/replacement/' # First matchsed 's/pattern/replacement/g' # All matchessed -E 's/pattern/replacement/' # Extended regexsed 's/\(group\)/\1/' # Backreference (BRE)sed -E 's/(group)/\1/' # Backreference (ERE)Tips & Gotchas
Section titled “Tips & Gotchas”-
Escape special characters in literals:
\. \* \+ \? \[ \] \( \) \{ \} \| \ \^ \$ -
Use raw strings in Python:
r'\d+'not'\d+' -
Anchors matter:
\d+matches digits anywhere;^\d+$matches only if entire string is digits -
Greedy by default:
.*eats as much as possible; use.*?for lazy -
Character class shortcuts:
[0-9]=\d,[a-zA-Z0-9_]=\w, `[
]=\s`
-
Test incrementally: Build complex patterns piece by piece
-
Use non-capturing groups
(?:...)when you don’t need the match -
Catastrophic backtracking: Avoid nested quantifiers like
(a+)+on long strings
See Also
Section titled “See Also”- Shell — grep, sed, awk patterns that use regex
- CLI Pipelines — Text processing with pipes
- Python —
remodule usage - Regex Lesson Plan — 8 lessons from literals to lookahead and extraction
- jq — JSON filtering with regex tests