Regex Cheatsheet

A quick reference for regular expression syntax: character classes, quantifiers, anchors, lookarounds, flags, and common patterns.

4 min read·Updated June 2026

A quick-reference for regular expression syntax. For a conceptual introduction, see the What is a Regular Expression? guide.

Character classes

TokenMatches
.Any character except newline (use /s flag to include newline)
\dDigit — [0-9]
\DNon-digit — [^0-9]
\wWord character — [a-zA-Z0-9_]
\WNon-word character
\sWhitespace — space, tab, newline, carriage return
\SNon-whitespace
[abc]Character class — a, b, or c
[^abc]Negated class — any character except a, b, or c
[a-z]Range — any lowercase letter
[a-zA-Z0-9]Alphanumeric

Quantifiers

TokenMeaningNote
*Zero or moreGreedy
+One or moreGreedy
?Zero or oneGreedy
{n}Exactly n times
{n,}At least n timesGreedy
{n,m}Between n and m timesGreedy
*?Zero or moreLazy
+?One or moreLazy

Add ? after any quantifier to make it lazy (match as few as possible): .*? .+? {n,m}?

Anchors & boundaries

TokenMatches at
^Start of string (or start of line with /m flag)
$End of string (or end of line with /m flag)
\bWord boundary — between \w and \W
\BNon-word boundary
\AStart of string (Python/Java — no /m effect)
\ZEnd of string (Python/Java — no /m effect)

Groups & alternation

TokenMeaning
(abc)Capturing group — captures the match as group 1, 2, …
(?:abc)Non-capturing group — groups without capturing
(?<name>abc)Named capturing group — accessible as match.groups.name
\1Backreference to group 1
\k<name>Backreference to named group
a|bAlternation — matches a or b

Lookaheads & lookbehinds

TokenMeaningExample
(?=...)Positive lookaheadfoo(?=bar) — "foo" followed by "bar"
(?!...)Negative lookaheadfoo(?!bar) — "foo" NOT followed by "bar"
(?<=...)Positive lookbehind(?<=\$)\d+ — digits preceded by "$"
(?<!...)Negative lookbehind(?<!un)happy — "happy" NOT preceded by "un"

Lookarounds are zero-width — they assert a condition without consuming characters.

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitive[a-z] also matches [A-Z]
mMultiline^ and $ match start/end of each line
sDot-all. also matches newline characters
uUnicodeEnables full Unicode matching and \u{XXXX} escapes
yStickyMatches only at lastIndex position (JS)

Common patterns

PatternRegexNotes
Email (simple)[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}Not RFC 5322 compliant — use a library for strict validation
URL (http/https)https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-\._~:/?#[\]@!$&'()*+,;=%]*)?Simple — does not handle all edge cases
IPv4 address(?:\d{1,3}\.){3}\d{1,3}Does not validate range (0-255)
IPv4 address (strict)(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)Validates 0-255 range
Phone (intl)\+?[\d\s\-().]{7,20}Flexible — adapt to your requirements
Date (YYYY-MM-DD)\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])Does not check calendar validity
Hex color#(?:[0-9a-fA-F]{3}){1,2}Matches #rgb and #rrggbb
Slug (URL-safe)[a-z0-9]+(?:-[a-z0-9]+)*Lowercase alphanumeric + hyphens
Semver\d+\.\d+\.\d+(?:-[\w.]+)?(?:\+[\w.]+)?Simplified — see official semver regex for full spec
UUID v4[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}Case-insensitive — add i flag
Credit card (Luhn)(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13})Structural only — use Luhn algorithm for real validation

Language differences

FeatureJavaScriptPythonGoJava
Syntax/pattern/flagsre.compile()regexp.MustCompile()Pattern.compile()
Lookahead
Lookbehind
Named groups(?<name>)(?P<name>)(?P<name>)(?<name>)
Non-greedy.*?.*?.*?.*?
Unicode class\p{L} (with v flag)\p{L}\p{L}\p{L}

Frequently asked questions