Regular Expression Cheatsheet
Quick reference for regex syntax covering basic patterns, quantifiers, anchors, groups, lookaround, character classes, flags, and common patterns
62 commands
.Match any single character (except newline)
a.c → abc, aXc\Escape metacharacter
\. → literal dot|Alternation (OR)
cat|dog → cat or dog\dDigit [0-9]
\d{3} → 123, 456\DNon-digit
\D+ → abc, xyz\wWord character [a-zA-Z0-9_]
\w+ → hello_world\WNon-word character
\W → @, #, !\sWhitespace (space, tab, newline)
\s+ → spaces/tabs\SNon-whitespace character
\S+ → hello\nNewline character
line1\nline2\tTab character
col1\tcol2*Match 0 or more times
ab*c → ac, abc, abbc+Match 1 or more times
ab+c → abc, abbc?Match 0 or 1 time
colou?r → color, colour{n}Match exactly n times
\d{4} → 2024{n,}Match n or more times
\d{2,} → 10, 100, 1000{n,m}Match between n and m times
\d{2,4} → 10, 100, 1000*?Lazy match (0 or more)
<.*?> → <b> in <b>text</b>+?Lazy match (1 or more)
".+?" → "a" in "a""b"??Lazy match (0 or 1)
colou??r → color first^Match beginning of line
^Hello → Hello at start$Match end of line
world$ → world at end\bMatch word boundary
\bcat\b → cat (not cats)\BMatch non-word boundary
\Bcat → concatenate\AMatch start of string
\AHello → Hello at very start\ZMatch end of string
end\Z → end at very end(pattern)Capturing group
(\d{4})-(\d{2})-(\d{2})(?:pattern)Non-capturing group
(?:http|https)://(?<name>pattern)Named capturing group
(?<year>\d{4})-(?<month>\d{2})\1, \2, ...Backreference to nth group
(\w+)\s\1 → the the\k<name>Named backreference
(?<word>\w+)\s\k<word>(a|b)Alternation within group
(cat|dog)s → cats, dogs(?=pattern)Positive lookahead
\d+(?= dollars) → 100 in "100 dollars"(?!pattern)Negative lookahead
\d+(?! dollars) → 100 in "100 yen"(?<=pattern)Positive lookbehind
(?<=\$)\d+ → 50 in "$50"(?<!pattern)Negative lookbehind
(?<!\$)\d+ → 50 in "50 items"[abc]Match any one character in set
[aeiou] → vowels[^abc]Match any character not in set
[^0-9] → non-digits[a-z]Lowercase letters
[a-z]+ → hello[A-Z]Uppercase letters
[A-Z]+ → HELLO[0-9]Digits
[0-9]{3} → 123[a-zA-Z]All letters
[a-zA-Z]+ → Hello[a-zA-Z0-9]Alphanumeric characters
[a-zA-Z0-9]+ → abc123[\s\S]Any character including newline
[\s\S]* → everythinggGlobal search (all matches)
/cat/g → all 'cat' occurrencesiCase-insensitive matching
/hello/i → Hello, HELLOmMultiline mode (^ $ match each line)
/^start/m → each line startsDotall mode (. matches newline)
/a.b/s → a\nbuUnicode mode
/\p{L}/u → Unicode lettersxExtended mode (ignore whitespace)
/\d{3} # area code/xEmailEmail address validation
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}URLURL validation
https?://[\w.-]+(?:\.[\w]{2,})(?:/[\w./?%&=-]*)?IPv4IPv4 address validation
\b(?:\d{1,3}\.){3}\d{1,3}\bDate (YYYY-MM-DD)Date format validation
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])Phone (Japan)Japanese phone number
0\d{1,4}-\d{1,4}-\d{4}Phone (US)US phone number
(?:\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}Password (Strong)Strong password (8+ chars, mixed)
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}HTML TagMatch HTML tag
<\/?[a-z][a-z0-9]*[^>]*>Hex ColorHex color code
#(?:[0-9a-fA-F]{3}){1,2}\bUUIDUUID validation
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}Whitespace TrimTrim leading/trailing whitespace
^\s+|\s+$Duplicate WordsDetect duplicate words
\b(\w+)\s+\1\b