Regex Without Tears

The 20% of regular expressions that solves 95% of problems — tokens, anchors, groups, lookarounds, flags.

Core tokens

. \d \w \s
any char · digit · word char [A-Za-z0-9_] · whitespace (capitals negate)
* + ? {n,m}
0+ · 1+ · 0/1 · between n and m — all greedy by default
Lazy: *? +?
shortest match — <.+?> stops at the first >
[abc] [^abc] [a-z]
set · negated set · range; inside sets most magic is literal
a|b
alternation — scope it with groups: gr(a|e)y

Anchors & groups

^ $ \b
line start · line end · word boundary (\bcat\b ≠ category)
(x) (?:x)
capturing vs non-capturing group
(?&lt;name&gt;x)
named capture → groups.name
\1 backreference
match the same text again — (\w+) \1 finds doubled words
Replace with $1
captured groups in replacements: (\d+)-(\d+)$2-$1

Lookarounds & flags

(?=x) (?!x)
lookahead: followed by / NOT followed by (zero-width)
(?&lt;=x) (?&lt;!x)
lookbehind: preceded by / not preceded by
Price example
(?<=\$)\d+ matches 42 in $42 without eating the $
Flags g i m s
all matches · ignore case · ^$ per line · dot matches newline

Battle-tested snippets

Trim
^\s+|\s+$ → replace with empty
Number with decimals
-?\d+(\.\d+)?
ISO date
\d{4}-\d{2}-\d{2}
Duplicate words
\b(\w+)\s+\1\b
The prime rule
if the regex needs comments, it should be code. Emails/URLs: use a library