Developer

Regular Expression Cheatsheet — Regex Quick Reference

Comprehensive regex cheatsheet with syntax, character classes, quantifiers, anchors, groups, lookahead/lookbehind, and common patterns.

Character Classes

PatternDescriptionExampleMatches
.Any character except newlinea.cabc, a1c, a-c
\dDigit (0-9)\d{3}123, 456
\DNon-digit\D+abc, ---
\wWord character (a-z, A-Z, 0-9, _)\w+hello_123
\WNon-word character\W!, @, #
\sWhitespace (space, tab, newline)a\sba b, a\tb
\SNon-whitespace\S+hello
[abc]Any of a, b, or c[aeiou]a, e, i
[^abc]Not a, b, or c[^aeiou]b, c, d
[a-z]Range: a to z[a-f]a, b, c, d, e, f
[A-Z]Range: A to Z[A-F]A, B, C, D, E, F
[0-9]Range: 0 to 9[0-5]0, 1, 2, 3, 4, 5

Quantifiers

PatternDescriptionExampleMatches
*0 or moreab*cac, abc, abbc
+1 or moreab+cabc, abbc
?0 or 1colou?rcolor, colour
{n}Exactly n\d{4}2024
{n,}n or more\d{2,}12, 123, 1234
{n,m}Between n and m\d{2,4}12, 123, 1234
*?0 or more (lazy)<.*?><b> in <b>bold</b>
+?1 or more (lazy)".+?""hi" in "hi" "bye"

Anchors & Boundaries

PatternDescriptionExampleMatches
^Start of string/line^HelloHello at start
$End of string/lineworld$world at end
\bWord boundary\bcat\bcat (not cats or concatenate)
\BNon-word boundary\Bcatscat, concatenate

Groups & References

PatternDescriptionExampleMatches
(abc)Capture group(ha)+ha, haha, hahaha
(?:abc)Non-capturing group(?:ha)+Same as above, no capture
(?<name>abc)Named capture group(?<year>\d{4})Captures year
\1Back reference to group 1(\w)\1aa, bb, cc
a|bAlternation (or)cat|dogcat or dog

Lookahead & Lookbehind

PatternDescriptionExampleMatches
(?=abc)Positive lookahead\d(?=px)3 in 3px
(?!abc)Negative lookahead\d(?!px)3 in 3em
(?<=abc)Positive lookbehind(?<=\$)\d+100 in $100
(?<!abc)Negative lookbehind(?<!\$)\d+100 in EUR100

Flags / Modifiers

FlagDescriptionExample
gGlobal search (find all matches)/hello/g
iCase-insensitive search/hello/i matches Hello
mMulti-line (^ and $ match line boundaries)/^start/m
sDotall (. matches newlines)/a.b/s matches a\nb
uUnicode support/\u{1F600}/u
ySticky (match from lastIndex)/\d/y

Common Patterns

PatternRegexNotes
Email^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$Basic email validation
URLhttps?:\/\/[\w.-]+(?:\.[\w]{2,})[\/\w.-]*HTTP/HTTPS URLs
IPv4 Address\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\be.g., 192.168.1.1
Hex Color#[0-9a-fA-F]{3,8}#fff, #FF5733, #FF573380
Phone (US)\(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4}(555) 123-4567
Date (YYYY-MM-DD)\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])2024-12-31
Time (HH:MM)(?:[01]\d|2[0-3]):[0-5]\d14:30, 09:15
Username^[a-zA-Z][\w]{2,15}$3-16 chars, starts with letter
Password (strong)^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w]).{8,}$Min 8, mixed case + digit + special
HTML Tag<\/?[a-z][\w-]*[^>]*><div>, </span>, <img />

Related Tools