Back to Cheat Sheets

Regex Cheat Sheet

A quick reference for regular expression patterns and syntax. Regex is supported in most programming languages, text editors, and command-line tools.

Character Classes

Pattern Description
.Any character except newline
dDigit (0-9)
wWord character (a-z, A-Z, 0-9, _)
sWhitespace (space, tab, newline)
DNon-digit
WNon-word character
SNon-whitespace
[abc]Any character in the set
[^abc]Any character not in the set
[a-z]Range of characters

Quantifiers

Pattern Description
a*Zero or more of "a"
a+One or more of "a"
a?Zero or one of "a"
a{n}Exactly "n" of "a"
a{n,}"n" or more of "a"
a{n,m}Between "n" and "m" of "a"
a*?Lazy — match as few as possible

Anchors

Pattern Description
^Start of string / line (multiline mode)
$End of string / line (multiline mode)
Word boundary
BNon-word boundary
AStart of string (ignores multiline mode)
ZEnd of string or before final newline
zAbsolute end of string

Groups and Lookaround

Pattern Description
(abc)Capturing group
(?:abc)Non-capturing group
\1Backreference to group 1
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

Flags

Pattern Description
gGlobal — find all matches, not just the first
iCase-insensitive matching
mMultiline — ^ and $ match line starts/ends
sDotall — "." matches newlines too
uUnicode — enable full Unicode matching
ySticky — match only from lastIndex

Common Examples

Pattern Description
^d{3}-d{4}$Phone (e.g. 123-4567)
[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}Email address (case-insensitive)
https?://[w./?-]+URL
^d{4}-d{2}-d{2}$Date (YYYY-MM-DD)
(?<=$)d+.?d*Price after $ sign