Regex Cheat Sheet
Quick reference for regular expression patterns. Searchable with examples. Click any pattern to copy.
Character Classes
Any character except newline
a.c โ abc, a1c, a-c
Digit (0-9)
\d+ โ 123, 42
Non-digit
\D+ โ abc, !@#
Word character (a-z, A-Z, 0-9, _)
\w+ โ hello_world
Non-word character
\W+ โ @#$, !!
Whitespace (space, tab, newline)
a\sb โ a b, a\tb
Non-whitespace
\S+ โ hello
Any character in the set
[aeiou] โ vowels
Any character NOT in the set
[^0-9] โ non-digits
Character range (lowercase)
[a-z]+ โ hello
Any letter
[A-Za-z]+ โ Hello
Quantifiers
0 or more
ab*c โ ac, abc, abbc
1 or more
ab+c โ abc, abbc (not ac)
0 or 1 (optional)
colou?r โ color, colour
Exactly n times
\d{3} โ 123, 456
n or more times
\d{2,} โ 12, 123, 1234
Between n and m times
\d{2,4} โ 12, 123, 1234
0 or more (lazy/non-greedy)
<.*?> โ shortest match
1 or more (lazy/non-greedy)
.+? โ single char at a time
Anchors & Boundaries
Start of string/line
^Hello โ starts with Hello
End of string/line
world$ โ ends with world
Word boundary
\bcat\b โ cat (not catch)
Non-word boundary
\Bcat โ concatenate
Exact match (anchored)
^\d+$ โ only digits
Groups & Alternation
Capturing group
(\d+) โ captures digits
Non-capturing group
(?:ab)+ โ ab, abab
Alternation (OR)
cat|dog โ cat or dog
Backreference to group 1
(\w+)\s\1 โ word word
Positive lookahead
\d(?=px) โ digit before px
Negative lookahead
\d(?!px) โ digit NOT before px
Positive lookbehind
(?<=\$)\d+ โ digits after $
Negative lookbehind
(?<!\$)\d+ โ digits NOT after $
Named capturing group
(?<year>\d{4}) โ named year
Flags
Global - find all matches
/abc/g โ all occurrences
Case-insensitive
/abc/i โ ABC, abc, Abc
Multiline - ^ and $ match line starts/ends
/^hello/m โ per line
Dotall - . matches newlines too
/a.b/s โ a\nb
Unicode support
/\p{L}/u โ any letter
Common Patterns
Email (basic)
user@example.com
URL (basic)
https://example.com/path
IPv4 address
192.168.1.1
Hex color
#ff5733, #ABC
Date (YYYY-MM-DD)
2024-01-15
Phone (E.164)
+14155551234
Strong password
Min 8, upper+lower+digit
