Regex Cheat Sheet
Regular expression syntax reference with a live tester: click any token to see what it matches.
A quick reference for regular expression syntax, with every row ready to try. The sheet covers character classes, anchors and word boundaries, quantifiers (greedy and lazy), groups and back-references, lookahead and lookbehind, escapes and common ready-made patterns, plus the flags. Click any row to load it into the tester at the top and see what it matches in sample text you can edit. The syntax shown is JavaScript's, which matches PCRE, Python and Java for nearly everything here.
Try it: click any row below to load it here
*? +? ??: Lazy: match as little as possible
Character classes
| Any character except a newline (with the s flag, newlines too) | c.t | |
| A digit, 0–9 | \d+ | |
| Anything except a digit | \D+ | |
| A word character: letter, digit or underscore | \w+ | |
| Anything except a word character | \W | |
| Whitespace: space, tab, newline | \s+ | |
| Anything except whitespace | \S+ | |
| Any one of a, b or c | gr[ae]y | |
| Any character except a, b or c | [^aeiou\s] | |
| Any character in the range a to z | [A-F0-9]{2} | |
| Any Unicode letter (needs the u flag) | \p{L}+ |
Anchors and boundaries
| Start of the string (start of each line with the m flag) | ^\w+ | |
| End of the string (end of each line with the m flag) | \w+$ | |
| Word boundary | \bcat\b | |
| Not a word boundary | \Bcat |
Quantifiers
| 0 or more | ab*c | |
| 1 or more | ab+c | |
| 0 or 1 (optional) | colou?r | |
| Exactly n times | \d{4} | |
| n or more times | \d{3,} | |
| Between n and m times | \w{3,5} | |
| Lazy: match as little as possible | <.+?> |
Groups and references
| Capturing group | (\d{3})-(\d{4}) | |
| Non-capturing group | (?:ha)+ | |
| Named capturing group | (?<year>\d{4})-(?<month>\d{2}) | |
| Back-reference to group 1 | \b(\w+) \1\b | |
| Alternation: a or b | cat|dog |
Lookaround
| Lookahead: followed by abc | \d+(?=px) | |
| Negative lookahead: not followed by abc | \d+(?!px|\d) | |
| Lookbehind: preceded by abc | (?<=\$)\d+(\.\d\d)? | |
| Negative lookbehind: not preceded by abc | (?<!\$)\b\d+ |
Escapes
| A literal special character | \d+\.\d+ | |
| Tab, newline, carriage return | \t | |
| A Unicode character by code (\u{1F600} with the u flag) | \u00e9 |
Common patterns
| Good enough for forms; full RFC 5322 is far longer | [\w.+-]+@[\w-]+(\.[\w-]+)+ | |
| http or https links | https?:\/\/[^\s/$.?#].[^\s]* | |
| Four numbers 0–255 separated by dots | \b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(… | |
| ISO 8601 date | \b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3… | |
| HH:MM | \b([01]\d|2[0-3]):[0-5]\d\b | |
| #RGB or #RRGGBB | #(?:[0-9a-fA-F]{3}){1,2}\b | |
| 5 digits, optional +4 | \b\d{5}(?:-\d{4})?\b | |
| 8+ chars with lower, upper, digit and symbol | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w… | |
| Leading or trailing spaces | ^\s+|\s+$ | |
| The same word twice in a row | \b(\w+)\s+\1\b |
Flags
| g | Global: find all matches, not just the first | |
|---|---|---|
| i | Ignore case | |
| m | Multiline: ^ and $ match at line breaks | |
| s | Dot-all: . also matches newlines | |
| u | Unicode: enables \p{…} and \u{…}, treats surrogate pairs as one character | |
| y | Sticky: match only at lastIndex | |
| d | Indices: report start and end positions of groups | |
| v | Unicode sets: set operations in classes, e.g. [\p{L}--[a-z]] |
Syntax shown is JavaScript's, which matches PCRE, Python and Java for nearly everything here. Lookbehind and \p{…} need a modern engine.
Common use cases
- Look up the syntax for a quantifier, group or lookaround
- Find a ready-made pattern for emails, dates or numbers
- Learn regex by trying each token on sample text
- Remind yourself what \b, \s or (?:…) mean
How to use
- Browse the sections, or search the page with Ctrl+F (Cmd+F on a Mac).
- Click a row to load its pattern into the tester.
- Edit the pattern, flags or test text to experiment.
- Move to the regex tester for longer tests and replacements.
FAQ
What's the difference between greedy and lazy?
Greedy quantifiers (*, +) match as much as possible; adding ? makes them lazy (*?, +?), matching as little as possible. <.+> matches a whole line of tags; <.+?> matches one tag.
Why do I need to escape some characters?
. * + ? ^ $ ( ) [ ] { } | \ and / have special meanings. Put a backslash before them to match the character itself, or use the regex escape tool.
Do these work in Excel or Google Sheets?
Google Sheets' REGEXMATCH uses RE2, which supports most of this but not lookarounds or back-references. Excel's newer REGEX functions use PCRE2.
Related tools
- Regex TesterTest and debug regular expressions with real-time match highlighting.
- JSON ValidatorValidate JSON syntax and structure.
- Cron Expression BuilderBuild cron expressions with interactive UI.
- Cron Expression ParserParse cron expressions to human-readable format.
- Time Unit ConverterConvert between time units (seconds, minutes, hours, days).
- Bandwidth CalculatorCalculate bandwidth and transfer times.