Metacharacters

CharDescriptionExample
.Any charactera.c → abc
^Start of string^hello
$End of stringworld$
*0 or moreab* → a, ab, abb
+1 or moreab+ → ab, abb
?0 or 1colou?r
\Escape\. → literal .
|Alternationcat|dog

Character Classes

ClassDescriptionEquivalent
\dDigit[0-9]
\DNon-digit[^0-9]
\wWord char[a-zA-Z0-9_]
\WNon-word[^a-zA-Z0-9_]
\sWhitespace[ \t\n\r]
\SNon-whitespace[^ \t\n\r]
\bWord boundary\bword\b

Quantifiers

QuantifierDescriptionExample
{n}Exactly n\d{3} → 123
{n,}n or more\d{2,} → 12, 123
{n,m}n to m\d{2,4} → 12-1234
*0 or moreSame as {0,}
+1 or moreSame as {1,}
?0 or 1Same as {0,1}
*?Lazy *Minimal match
+?Lazy +Minimal match

Groups & Lookaround

SyntaxDescription
(abc)Capturing group
(?:abc)Non-capturing group
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?Negative lookbehind
\1Backreference to group 1
(?<name>)Named group

Bracket Expressions

ExpressionDescriptionExample Match
[abc]Any of a, b, or ca, b, c
[^abc]Not a, b, or cd, e, 1
[a-z]Lowercase lettersa, m, z
[A-Z]Uppercase lettersA, M, Z
[0-9]Digits0, 5, 9
[a-zA-Z]All lettersa, Z
[a-zA-Z0-9]Alphanumerica, Z, 5

Common Patterns

Email ^[\w.+-]+@[\w.-]+\.[a-zA-Z]{2,}$ user@example.com
URL https?://[\w.-]+\.[a-z]{2,} https://example.com
Phone (US) \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} (555) 123-4567
IP Address \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} 192.168.1.1
Date (YYYY-MM-DD) \d{4}-\d{2}-\d{2} 2025-01-27
Time (24h) ([01]?\d|2[0-3]):[0-5]\d 23:59
Hex Color #?[A-Fa-f0-9]{6} #FF5733
UUID [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} UUID v4 format
ZIP Code \d{5}(-\d{4})? 12345, 12345-6789
Username ^[a-zA-Z0-9_]{3,16}$ 3-16 chars, alphanumeric
Strong Password (?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,} Upper, lower, digit, 8+
Credit Card \d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4} 1234-5678-9012-3456

Flags / Modifiers

FlagDescription
iCase insensitive
gGlobal (find all)
mMultiline mode
sDot matches newline
uUnicode support
xExtended (ignore whitespace)

Quick Reference

^\s+Leading whitespace
\s+$Trailing whitespace
\s{2,}Multiple spaces
<[^>]+>HTML tags
#[a-zA-Z0-9_]+Hashtags
@[a-zA-Z0-9_]+@mentions