Back to Cheat Sheets Regex Cheat Sheet
A quick reference for regular expression patterns and syntax. Regex is supported in most programming languages, text editors, and command-line tools.
Character Classes
| Pattern | Description |
|---|
| . | Any character except newline |
| d | Digit (0-9) |
| w | Word character (a-z, A-Z, 0-9, _) |
| s | Whitespace (space, tab, newline) |
| D | Non-digit |
| W | Non-word character |
| S | Non-whitespace |
| [abc] | Any character in the set |
| [^abc] | Any character not in the set |
| [a-z] | Range of characters |
Quantifiers
| Pattern | Description |
|---|
| a* | Zero or more of "a" |
| a+ | One or more of "a" |
| a? | Zero or one of "a" |
| a{n} | Exactly "n" of "a" |
| a{n,} | "n" or more of "a" |
| a{n,m} | Between "n" and "m" of "a" |
| a*? | Lazy — match as few as possible |
Anchors
| Pattern | Description |
|---|
| ^ | Start of string / line (multiline mode) |
| $ | End of string / line (multiline mode) |
| Word boundary |
| B | Non-word boundary |
| A | Start of string (ignores multiline mode) |
| Z | End of string or before final newline |
| z | Absolute end of string |
Groups and Lookaround
| Pattern | Description |
|---|
| (abc) | Capturing group |
| (?:abc) | Non-capturing group |
| \1 | Backreference to group 1 |
| (?=abc) | Positive lookahead |
| (?!abc) | Negative lookahead |
| (?<=abc) | Positive lookbehind |
| (?<!abc) | Negative lookbehind |
Flags
| Pattern | Description |
|---|
| g | Global — find all matches, not just the first |
| i | Case-insensitive matching |
| m | Multiline — ^ and $ match line starts/ends |
| s | Dotall — "." matches newlines too |
| u | Unicode — enable full Unicode matching |
| y | Sticky — match only from lastIndex |
Common Examples
| Pattern | Description |
|---|
| ^d{3}-d{4}$ | Phone (e.g. 123-4567) |
| [A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,} | Email address (case-insensitive) |
| https?://[w./?-]+ | URL |
| ^d{4}-d{2}-d{2}$ | Date (YYYY-MM-DD) |
| (?<=$)d+.?d* | Price after $ sign |