Regular Expressions Pocket Guide
A quick reference for Regular Expressions. Write powerful patterns to match, search, and replace text.
Character Classes
| Key / Code | Description |
|---|---|
| . | Any character (except newline) |
| \d | Digit (0-9) |
| \w | Word character (a-z, A-Z, 0-9, _) |
| \s | Whitespace (space, tab, newline) |
| [abc] | Any of a, b, or c |
| [^abc] | Not a, b, or c |
| [a-z] | Character range a to z |
Quantifiers
| Key / Code | Description |
|---|---|
| * | 0 or more |
| + | 1 or more |
| ? | 0 or 1 (optional) |
| {3} | Exactly 3 times |
| {2,5} | Between 2 and 5 times |
Anchors & Groups
| Key / Code | Description |
|---|---|
| ^ | Start of string/line |
| $ | End of string/line |
| \b | Word boundary |
| (...) | Group |
| (?:...) | Non-capturing group |
Common Patterns
| Key / Code | Description |
|---|---|
| ^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$ | |
| ISO Date (YYYY-MM-DD) | ^\d{4}-\d{2}-\d{2}$ |
| Hex Color | ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ |
Knowledge is power.