PCRE, JS, Python, Java
Regex Cheat Sheet Reference
The regular expression metacharacters, quantifiers, character classes, and anchors you reach for most, shared across common engines.
At a glance
Key factsDot matches one
Star, plus, question mark
Backslash before a token
Common tokens
16 rows| Token | Matches |
|---|---|
| . | Any single character |
| ^ | Start of the string or line |
| $ | End of the string or line |
| * | Zero or more of the previous |
| + | One or more of the previous |
| ? | Zero or one (optional) |
| \d | A digit, 0 to 9 |
| \w | A word character, letter, digit, or underscore |
| \s | Any whitespace |
| [abc] | Any one of a, b, or c |
| [^abc] | Any character except a, b, or c |
| [a-z] | Any character in the range |
| a|b | Either a or b |
| (...) | A capturing group |
| {2,4} | Between two and four of the previous |
| \b | A word boundary |
Where it comes from
Regular expressions describe text patterns using a compact syntax that grew out of formal language theory and Unix tools in the 1970s and 1980s. Most modern engines, including PCRE, JavaScript, Python, and Java, share a common core of syntax, which is what the tokens below cover. Individual engines add their own extensions, but the pieces here behave the same nearly everywhere, so they are safe to learn first.
How to use it
A pattern is read left to right, matching literal characters unless a token has special meaning. A dot matches any single character, and quantifiers set how many times the previous item may repeat: star means zero or more, plus means one or more, question mark means optional. Square brackets define a set of allowed characters, and a caret inside them negates the set. Anchors do not match characters but positions: caret is the start of a line and dollar the end. To match a metacharacter literally, put a backslash in front of it, for example a backslash and a dot to match a real full stop.
This page is a standing reference at a fixed URL, built to be linked and cited. The data here is compiled from common regular expression syntax shared by PCRE, JavaScript, and Python.