Cheat Sheet
Regular Expression Cheat Sheet
A quick reference for the most common regex syntax. Patterns shown use PCRE/JavaScript flavor.
Character Classes
| Pattern | Matches |
. | Any character except newline |
\d / \D | Digit / non-digit |
\w / \W | Word character (a-zA-Z0-9_) / non-word |
\s / \S | Whitespace / non-whitespace |
[abc] | Any of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Range a through z |
Anchors & Boundaries
^ | Start of string (or line in multiline mode) |
$ | End of string (or line in multiline mode) |
\b / \B | Word boundary / non-boundary |
\A / \Z | Start / end of input (PCRE) |
Quantifiers
* | 0 or more (greedy) |
+ | 1 or more |
? | 0 or 1 (also makes a quantifier lazy) |
{n} | Exactly n times |
{n,} | n or more |
{n,m} | Between n and m times |
*? +? {n,m}? | Lazy versions |
Groups & Alternation
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
a|b | a or b |
\1 .. \9 | Backreference to capture group |
Lookaround
(?=abc) | Positive lookahead |
(?!abc) | Negative lookahead |
(?<=abc) | Positive lookbehind |
(?<!abc) | Negative lookbehind |
Common Flags
i | Case-insensitive |
g | Global (find all matches) |
m | Multiline (^ and $ match line boundaries) |
s | Dotall (. matches newlines) |
u | Unicode (JavaScript) |