Regex Cheat Sheet

Quick reference for regular expression syntax

Character Classes

.Match any character except newline
\dMatch any digit (0-9)
\DMatch any non-digit character
\wMatch any word character (a-z, A-Z, 0-9, _)
\WMatch any non-word character
\sMatch any whitespace character
\SMatch any non-whitespace character
[abc]Match any character in the set
[^abc]Match any character NOT in the set
[a-z]Match any character in the range

Anchors

^Match the start of the string
$Match the end of the string
\bMatch a word boundary
\BMatch a non-word boundary

Quantifiers

*Match 0 or more times
+Match 1 or more times
?Match 0 or 1 time
{n}Match exactly n times
{n,}Match at least n times
{n,m}Match between n and m times
*?Match 0 or more (lazy)
+?Match 1 or more (lazy)
??Match 0 or 1 (lazy)

Groups & Lookaround

(...)Capturing group
(?:...)Non-capturing group
(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind
(?<!...)Negative lookbehind
\1, \2, etc.Backreference to group
(?<name>...)Named capturing group

Alternation

a|bMatch either a or b
(a|b)Group with alternation

Escaped Characters

\.Match literal dot
\\Match literal backslash
\nMatch newline
\rMatch carriage return
\tMatch tab
\uFFFFMatch Unicode character
\xHHMatch hex character

Flags

gGlobal search (find all matches)
iCase-insensitive search
mMulti-line mode (^ and $ match line boundaries)
sDot-all mode (. matches newline)
uUnicode mode
ySticky mode