# Complete REGEX Cheatsheet
## Table of Contents
- [Basic Syntax](#basic-syntax)
- [Character Classes](#character-classes)
- [Quantifiers](#quantifiers)
- [Anchors & Boundaries](#anchors--boundaries)
- [Groups & Capturing](#groups--capturing)
- [Lookaheads & Lookbehinds](#lookaheads--lookbehinds)
- [Flags/Modifiers](#flagsmodifiers)
- [Special Characters](#special-characters)
- [Common Patterns](#common-patterns)
- [Examples by Use Case](#examples-by-use-case)
---
## Basic Syntax
| Pattern | Description | Example | Matches |
|---------|-------------|---------|---------|
| `.` | Any character (except newline) | `a.c` | "abc", "axc", "a1c" |
| `\` | Escape special character | `\.` | literal "." |
| `|` | OR operator | `cat|dog` | "cat" or "dog" |
| `[]` | Character set | `[abc]` | "a", "b", or "c" |
| `[^]` | Negated character set | `[^abc]` | Any character except "a", "b", "c" |
---
## Character Classes
| Pattern | Description | Equivalent |
|---------|-------------|------------|
| `\d` | Digit | `[0-9]` |
| `\D` | Non-digit | `[^0-9]` |
| `\w` | Word character | `[a-zA-Z0-9_]` |
| `\W` | Non-word character | `[^a-zA-Z0-9_]` |
| `\s` | Whitespace | `[ \t\n\r\f\v]` |
| `\S` | Non-whitespace | `[^ \t\n\r\f\v]` |
### Custom Character Classes
| Pattern | Description |
|---------|-------------|
| `[a-z]` | Lowercase letters |
| `[A-Z]` | Uppercase letters |
| `[0-9]` | Digits |
| `[a-zA-Z]` | All letters |
| `[a-zA-Z0-9]` | Alphanumeric |
| `[aeiou]` | Vowels |
| `[^aeiou]` | Consonants |
---
## Quantifiers
| Pattern | Description | Example | Matches |
|---------|-------------|---------|---------|
| `*` | 0 or more | `ab*` | "a", "ab", "abb", "abbb" |
| `+` | 1 or more | `ab+` | "ab", "abb", "abbb" |
| `?` | 0 or 1 (optional) | `ab?` | "a", "ab" |
| `{n}` | Exactly n times | `a{3}` | "aaa" |
| `{n,}` | n or more times | `a{3,}` | "aaa", "aaaa", "aaaaa" |
| `{n,m}` | Between n and m times | `a{2,4}` | "aa", "aaa", "aaaa" |
### Lazy Quantifiers (Non-greedy)
| Pattern | Description |
|---------|-------------|
| `*?` | 0 or more (lazy) |
| `+?` | 1 or more (lazy) |
| `??` | 0 or 1 (lazy) |
| `{n,m}?` | Between n and m (lazy) |
---
## Anchors & Boundaries
| Pattern | Description | Example | Matches |
|---------|-------------|---------|---------|
| `^` | Start of line | `^Hello` | "Hello" at start |
| `$` | End of line | `world$` | "world" at end |
| `\b` | Word boundary | `\bcat\b` | "cat" as whole word |
| `\B` | Non-word boundary | `\Bcat\B` | "cat" within word |
| `\A` | Start of string | `\AHello` | "Hello" at very start |
| `\Z` | End of string | `world\Z` | "world" at very end |
---
## Groups & Capturing
| Pattern | Description | Example |
|---------|-------------|---------|
| `()` | Capturing group | `(abc)+` |
| `(?:)` | Non-capturing group | `(?:abc)+` |
| `(?<name>)` | Named capturing group | `(?<year>\d{4})` |
| `\1` | Backreference to group 1 | `(\w+)\s+\1` |
| `\k<name>` | Named backreference | `(?<word>\w+)\s+\k<word>` |
---
## Lookaheads & Lookbehinds
| Pattern | Description | Example |
|---------|-------------|---------|
| `(?=)` | Positive lookahead | `\d+(?=px)` |
| `(?!)` | Negative lookahead | `\d+(?!px)` |
| `(?<=)` | Positive lookbehind | `(?<=\$)\d+` |
| `(?<!)` | Negative lookbehind | `(?<!\$)\d+` |
---
## Flags/Modifiers
| Flag | Description | Usage |
|------|-------------|--------|
| `i` | Case insensitive | `/pattern/i` |
| `g` | Global match | `/pattern/g` |
| `m` | Multiline mode | `/pattern/m` |
| `s` | Dot matches newline | `/pattern/s` |
| `x` | Extended/verbose | `/pattern/x` |
| `u` | Unicode | `/pattern/u` |
---
## Special Characters
Characters that need escaping: `. ^ $ * + ? { } [ ] \ | ( )`
| Literal | Escaped |
|---------|---------|
| `.` | `\.` |
| `^` | `\^` |
| `$` | `\$` |
| `*` | `\*` |
| `+` | `\+` |
| `?` | `\?` |
| `{` | `\{` |
| `}` | `\}` |
| `[` | `\[` |
| `]` | `\]` |
| `\` | `\\` |
| `|` | `\|` |
| `(` | `\(` |
| `)` | `\)` |
---
## Common Patterns
### Email
```regex
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
```
### Phone Numbers
```regex
# US Format: (123) 456-7890
^\(\d{3}\)\s\d{3}-\d{4}$
# International: +1-234-567-8900
^\+\d{1,3}-\d{3}-\d{3}-\d{4}$
```
### URLs
```regex
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$
```
### IP Address
```regex
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
```
### Date Formats
```regex
# YYYY-MM-DD
^\d{4}-\d{2}-\d{2}$
# MM/DD/YYYY
^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$
# DD/MM/YYYY
^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}$
```
### Time
```regex
# 24-hour format: HH:MM
^([01]?[0-9]|2[0-3]):[0-5][0-9]$
# 12-hour format: HH:MM AM/PM
^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$
```
### Credit Card
```regex
# Visa
^4[0-9]{12}(?:[0-9]{3})?$
# Mastercard
^5[1-5][0-9]{14}$
# American Express
^3[47][0-9]{13}$
```
### Social Security Number
```regex
^\d{3}-\d{2}-\d{4}$
```
### Postal Codes
```regex
# US ZIP Code
^\d{5}(-\d{4})?$
# UK Postal Code
^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}$
# Canadian Postal Code
^[A-Z]\d[A-Z] ?\d[A-Z]\d$
```
---
## Examples by Use Case
### Validation
```regex
# Strong Password (8+ chars, uppercase, lowercase, number, special char)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
# Username (3-16 chars, alphanumeric and underscore)
^[a-zA-Z0-9_]{3,16}$
# Hex Color Code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
```
### Data Extraction
```regex
# Extract numbers from text
\d+(\.\d+)?
# Extract words
\b[A-Za-z]+\b
# Extract quoted strings
"([^"]*)"
# Extract HTML tags
<\/?[^>]+>
# Extract file extensions
\.([a-zA-Z0-9]+)$
```
### Text Processing
```regex
# Remove extra whitespace
\s+
# Match whole words
\b\w+\b
# Match lines starting with specific text
^pattern.*$
# Match content between parentheses
\(([^)]+)\)
# Match content between brackets
\[([^\]]+)\]
```
### Advanced Patterns
```regex
# Balanced parentheses (simple case)
\([^)]*\)
# Remove comments (// style)
\/\/.*$
# Match floating point numbers
-?\d+(\.\d+)?([eE][+-]?\d+)?
# Match words with specific prefixes
\b(pre|post|anti|pro)\w*
# Match repeated words
\b(\w+)\s+\1\b
```
---
## Quick Tips
1. **Test your regex**: Use online tools like regex101.com or regexr.com
2. **Start simple**: Build complex patterns step by step
3. **Use non-capturing groups**: `(?:...)` when you don't need to capture
4. **Be specific**: `\d` instead of `.` when you want digits
5. **Use anchors**: `^` and `$` for exact matches
6. **Escape special characters**: Use `\` before special characters when literal
7. **Consider performance**: Avoid excessive backtracking with lazy quantifiers
8. **Use character classes**: `[abc]` is more efficient than `(a|b|c)`
---
*This cheatsheet covers the most commonly used regex patterns. For language-specific variations or advanced features, refer to your programming language's regex documentation.*