Regular expressions (regex) are patterns used to match character combinations in strings. This cheatsheet covers common regex syntax with working examples.
Basic Patterns
- `.` - Match any single character (except newline)
Example: `h.t` matches "hat", "hot", "hit"
- `[abc]` - Match any character in brackets
Example: `[aeiou]` matches any vowel
- `[^abc]` - Match any character NOT in brackets
Example: `[^aeiou]` matches any consonant
- `[a-z]` - Match any character in range
Example: `[0-9]` matches any digit
Quantifiers
- `*` - Zero or more of preceding element
Example: `ab*c` matches "ac", "abc", "abbc", "abbbc"
- `+` - One or more of preceding element
Example: `ab+c` matches "abc", "abbc" but not "ac"
- `?` - Zero or one of preceding element (optional)
Example: `colou?r` matches both "color" and "colour"
- `{n}` - Exactly n occurrences
Example: `\d{3}` matches exactly 3 digits
- `{n,}` - n or more occurrences
Example: `\d{3,}` matches 3 or more digits
- `{n,m}` - Between n and m occurrences
Example: `\d{2,4}` matches 2 to 4 digits
Anchors
- `^` - Start of string (or start of line in multiline mode)
Example: `^Hello` matches "Hello" only at the start
- `$` - End of string (or end of line in multiline mode)
Example: `world$` matches "world" only at the end
- `\b` - Word boundary
Example: `\bcat\b` matches "cat" but not "category"
- `\B` - Non-word boundary
Example: `\Bcat\B` matches "category" but not "cat"
Character Classes
- `\d` - Any digit (equivalent to `[0-9]`)
Example: `\d{4}` matches "2024", "1234"
- `\D` - Any non-digit (equivalent to `[^0-9]`)
Example: `\D+` matches "abc", "xyz"
- `\w` - Any word character (letters, digits, underscore)
Example: `\w+` matches "hello", "test123", "my_var"
- `\W` - Any non-word character
Example: `\W+` matches "!!!", " ", "---"
- `\s` - Any whitespace character (space, tab, newline)
Example: `\s+` matches spaces, tabs
- `\S` - Any non-whitespace character
Example: `\S+` matches "hello", "123"
Groups and Capturing
- `(abc)` - Capture group
Example: `(hello)` captures "hello" for backreference
- `(?:abc)` - Non-capturing group
Example: `(?:hello|hi)` matches but doesn't capture
- `|` - Alternation (OR)
Example: `cat|dog` matches "cat" or "dog"
- `\1, \2, ...` - Backreferences to captured groups
Example: `(\w)\1` matches "aa", "bb", "11"
Special Characters (Escaping)
- `\.` - Literal period
Example: `\.com` matches ".com"
- `\+` - Literal plus sign
Example: `\+1` matches "+1"
- `\*` - Literal asterisk
Example: `\*note` matches "*note"
- `\?` - Literal question mark
Example: `really\?` matches "really?"
- `\(` `\)` - Literal parentheses
Example: `\(test\)` matches "(test)"
Common Patterns
Email Address (Simple)
[\w.-]+@[\w.-]+\.\w+
Matches: `user@example.com`, `test.email@domain.co.uk`
Phone Number (US Format)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches: `(555)123-4567`, `555-123-4567`, `555.123.4567`
URL
https?://[\w.-]+(?:\.[\w.-]+)*(?:/[\w./?=&#-]*)?
Matches: `https://example.com`, `http://site.com/path?q=1`
IP Address
\b(?:\d{1,3}\.){3}\d{1,3}\b
Matches: `192.168.1.1`, `10.0.0.1`
Date (MM/DD/YYYY)
\d{2}/\d{2}/\d{4}
Matches: `12/25/2024`, `01/01/2025`
Flags/Modifiers (Common)
- `i` - Case-insensitive matching
Example: `/hello/i` matches "Hello", "HELLO", "hello"
- `g` - Global matching (find all, not just first)
Example: `/cat/g` finds all occurrences of "cat"
- `m` - Multiline mode (^ and $ match line boundaries)
Example: `/^line/m` matches "line" at start of any line
- `s` - Dotall mode (. matches newline)
Example: `/hello.world/s` matches "hello\nworld"
Tips
- Test regex patterns with online tools like regex101.com or regexr.com
- Escape special characters with backslash when you want literal matches
- Use non-greedy quantifiers (`*?`, `+?`) for minimal matches
- Group related patterns with parentheses for better organization
- Use anchors (`^`, `$`) to ensure full string matching
- Different tools/languages may have slightly different regex syntax
- Common uses: validation, search/replace, parsing, extraction
- Start simple and build complexity incrementally
- Document complex regex patterns with comments if your tool supports it