Cheat Sheet

Regular Expression Cheat Sheet

A quick reference for the most common regex syntax. Patterns shown use PCRE/JavaScript flavor.

Character Classes

PatternMatches
.Any character except newline
\d / \DDigit / non-digit
\w / \WWord character (a-zA-Z0-9_) / non-word
\s / \SWhitespace / 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 / \BWord boundary / non-boundary
\A / \ZStart / 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|ba or b
\1 .. \9Backreference to capture group

Lookaround

(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

Common Flags

iCase-insensitive
gGlobal (find all matches)
mMultiline (^ and $ match line boundaries)
sDotall (. matches newlines)
uUnicode (JavaScript)