Regex Cheatsheet
Quick reference guide for regular expressions.
Characters
| . | Any character except newline |
| \w | Word character [a-zA-Z0-9_] |
| \d | Digit [0-9] |
| \s | Whitespace |
| \W | Non-word character |
| \D | Non-digit |
| \S | Non-whitespace |
Quantifiers
| * | 0 or more |
| + | 1 or more |
| ? | 0 or 1 (optional) |
| {n} | Exactly n times |
| {n,} | n or more times |
| {n,m} | Between n and m times |
Anchors
| ^ | Start of string/line |
| $ | End of string/line |
| \b | Word boundary |
| \B | Non-word boundary |
Groups & Lookaround
| (abc) | Capture group |
| (?:abc) | Non-capturing group |
| (?=abc) | Positive lookahead |
| (?!abc) | Negative lookahead |
| a|b | Alternation (or) |
Character Sets
| [abc] | Match a, b, or c |
| [^abc] | Not a, b, or c |
| [a-z] | Range: a to z |
| [A-Z] | Range: A to Z |
| [0-9] | Range: 0 to 9 |
Flags
| g | Global (all matches) |
| i | Case-insensitive |
| m | Multiline |
| s | Dotall (dot matches \n) |
Common Patterns
| Pattern | Description |
|---|---|
| ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | Email address |
| ^https?:\/\/[^\s]+$ | URL |
| ^\d{3}-\d{3}-\d{4}$ | Phone (US) |
| ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ | IPv4 address |
| ^\d{4}-\d{2}-\d{2}$ | Date (YYYY-MM-DD) |