Regex Cheatsheet

Every regex syntax element with examples, plus common patterns.

By Λ · Updated May 18, 2026

Test patterns interactively in the regex tester. For the conceptual walkthrough, see my regex guide post.

Character Classes

PatternMatchesExample
.Any character except newlinea.c matches "abc", "aXc"
\dAny digit (0-9)\d{3} matches "123"
\DAny non-digit\D+ matches "abc!"
\wWord character [A-Za-z0-9_]\w+ matches "hello_123"
\WNon-word character\W matches space, ! etc.
\sWhitespace (space, tab, newline)\s+ matches " "
\SNon-whitespaceeverything but whitespace
[abc]Any of a, b, or c[aeiou] matches a vowel
[^abc]Any except a, b, c[^0-9] matches non-digit
[a-z]Range a through z[A-Za-z0-9] alphanumeric

Quantifiers

PatternMatchesNotes
*0 or moreGreedy by default
+1 or moreGreedy
?0 or 1 (optional)Or makes the previous quantifier lazy
{n}Exactly n\d{3} exactly 3 digits
{n,}n or more\d{3,} 3+ digits
{n,m}Between n and m\d{3,5} 3 to 5 digits
*?Lazy: 0 or more, as few as possibleUse to avoid backtracking
+?Lazy: 1 or more

Anchors

PatternMatchesExample
^Start of string (or line in multiline mode)^hello
$End of string (or line)world$
\bWord boundary\bcat\b matches "cat" not "category"
\BNon-word-boundary\Bcat matches "scattered" inner cat

Groups and Captures

PatternMatchesExample
(abc)Capturing group(\d{3})-(\d{4}) captures area + number
(?:abc)Non-capturing groupFor grouping without saving
(?<name>abc)Named capturing groupRefer back as \k<name>
\1, \2, \3Backreference to capture group(a)\1 matches "aa"
|Alternation (OR)cat|dog matches either

Lookarounds

PatternMatchesExample
(?=abc)Positive lookahead: followed by abc\d+(?=px) match digits before "px"
(?!abc)Negative lookahead: NOT followed by abc\d+(?!px)
(?<=abc)Positive lookbehind: preceded by abc(?<=\$)\d+ match digits after $
(?<!abc)Negative lookbehind: NOT preceded by abc

Flags / Modifiers

FlagEffect
iCase-insensitive
gGlobal (find all, not just first)
mMultiline (^ and $ match line boundaries)
sDotall (. also matches newline)
uUnicode mode
xExtended (ignore whitespace in pattern, allow comments) [some engines]

Common Patterns

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Email (RFC-incomplete but works for 99% of real addresses)
^https?:\/\/[^\s/$.?#].[^\s]*$
URL (HTTP/HTTPS)
^\+?[1-9]\d{1,14}$
Phone number (E.164 international format)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Date YYYY-MM-DD
^([01]?\d|2[0-3]):[0-5]\d(:[0-5]\d)?$
Time HH:MM or HH:MM:SS (24-hour)
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Hex color (#abc or #aabbcc)
^(?:[a-zA-Z0-9+\/]{4})*(?:[a-zA-Z0-9+\/]{2}==|[a-zA-Z0-9+\/]{3}=)?$
Base64
^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$
IPv4 (loose; for strict use the subnet calc)
^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$
UUID
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)
IPv4 (strict, validates each octet 0-255)
(?<=\$)\d+(\.\d{2})?
US dollar amount preceded by $
^\d{3}-\d{2}-\d{4}$
US Social Security Number format
\b[A-Z][A-Za-z0-9_]*\b
Class name (PascalCase identifier)
\b[a-z][a-zA-Z0-9]*\b
camelCase identifier
^[a-z0-9]+(-[a-z0-9]+)*$
Slug (lowercase, hyphenated)

Common Gotchas

Last updated