Regex Cheatsheet
The syntax of regular expressions on one page, each item with an example you can open in the Regex Tester, and ready-made patterns written out for six languages.
Open Regex Tester Type a pattern and some text to see what it matches.
Characters and classes
| . | Any character except a line break (with s, line breaks too). | c.t (open in Regex Tester) |
|---|---|---|
| \d | A digit. In JavaScript only 0-9; in Python 3 any Unicode digit. | \d+ (open in Regex Tester) |
| \w | A word character: letters, digits, underscore (ASCII in JavaScript). | \w+ (open in Regex Tester) |
| \s | Whitespace: space, tab, line breaks and more. | \s+ (open in Regex Tester) |
| \D \W \S | The opposite of \d, \w, \s. | \D+ (open in Regex Tester) |
| [abc] | One of the listed characters. | gr[ae]y (open in Regex Tester) |
| [^abc] | Any character except the listed ones. | [^aeiou\s]+ (open in Regex Tester) |
| [a-z] | A range. Combine them: [A-Za-z0-9_]. | [A-F0-9]+ (open in Regex Tester) |
| \b | A word boundary: between \w and something else. | \bcat\b (open in Regex Tester) |
Anchors
| ^ | Start of the string (with m, of each line). | ^\w+ (open in Regex Tester) |
|---|---|---|
| $ | End of the string (with m, of each line). | \w+$ (open in Regex Tester) |
| \A \z | Start and end of the whole string, whatever the flags, in Java, PHP, Go and .NET. Python writes the end as \Z (\z too from 3.14). JavaScript has neither; use ^ and $ without m. |
Quantifiers
| * | Zero or more. | ab*c (open in Regex Tester) |
|---|---|---|
| + | One or more. | ab+c (open in Regex Tester) |
| ? | Zero or one: makes the item optional. | colou?r (open in Regex Tester) |
| {3} | Exactly 3. | \d{3} (open in Regex Tester) |
| {2,} | 2 or more. | \d{2,} (open in Regex Tester) |
| {2,4} | From 2 to 4. | \d{2,4} (open in Regex Tester) |
| *? +? ?? | Lazy: as few as possible. <.+?> stops at the first >. | <.+?> (open in Regex Tester) |
Groups and lookaround
| (abc) | A capturing group: matched text is kept as $1, \1, group(1). | (\d{4})-(\d{2}) (open in Regex Tester) |
|---|---|---|
| (?:abc) | A group that does not capture: for alternation and quantifiers. | (?:ab)+ (open in Regex Tester) |
| (?<year>\d{4}) | A named group. Python's re writes it (?P<year>...). | (?<year>\d{4}) (open in Regex Tester) |
| a|b | Either side. | cat|dog (open in Regex Tester) |
| \1 | A backreference: the text group 1 matched. Not in Go. | (\w)\1 (open in Regex Tester) |
| (?=...) | Followed by, without consuming it. Not in Go. | \d+(?=px) (open in Regex Tester) |
| (?!...) | Not followed by. Not in Go. | \d+(?!px|\d) (open in Regex Tester) |
| (?<=...) | Preceded by. Not in Go. | (?<=\$)\d+ (open in Regex Tester) |
| (?<!...) | Not preceded by. Not in Go. | (?<![\$\d])\d+ (open in Regex Tester) |
Flags in JavaScript
| g | Global: find every match, not just the first. |
|---|---|
| i | Ignore case. |
| m | Multiline: ^ and $ match at line breaks. |
| s | Dot-all: . matches line breaks too. |
| u | Unicode: code points instead of UTF-16 units, and \p{...} classes. |
| v | Like u, plus set operations in classes. Use one of u or v. |
| y | Sticky: match only at lastIndex. |
| d | Indices: report where each group starts and ends. |
Characters to escape
Outside a class, put a backslash before any of . * + ? ^ $ ( ) [ ] { } | \ to match it literally, and before / inside a JavaScript literal. Inside [...], only ] \ ^ - need it, and only where they would otherwise mean something. With the v flag, a class also needs a backslash before ( ) [ ] { } / - |, and doubled punctuation such as && or !! is reserved.
Flavors differ
- Go (RE2) has no lookaround and no backreferences. In exchange every match runs in linear time.
- Python's
renames groups(?P<name>...); JavaScript, Java and .NET write(?<name>...). \dis0-9in JavaScript, but any Unicode digit in Python 3 and .NET unless you ask for ASCII.- Python's
re.matchanchors only at the start;re.fullmatchand Java'smatches()test the whole string.
Ready-made patterns
16 guides