# Python
import re
re.sub(r"(\d+)-(\d+)-(\d+)", r"\3.\2.\1", "2026-11-27") # '27.11.2026'
re.findall(r"#[0-9a-fA-F]{6}\b", css) # all hex colours
Lookarounds
Lookarounds check what is next to the current position without including it in the match.
Syntax
Name
Example
(?=…)
Lookahead
\d+(?=px) — numbers followed by “px”
(?!…)
Negative lookahead
\bfoo(?!bar) — “foo” not followed by “bar”
(?<=…)
Lookbehind
(?<=\$)\d+ — amount after a dollar sign
(?<!…)
Negative lookbehind
(?<!-)\b\d+ — numbers without a minus
# at least 8 chars, one digit and one lowercase letter
^(?=.*\d)(?=.*[a-z]).{8,}$
Handy recipes
Task
Pattern
ISO date
\b\d{4}-\d{2}-\d{2}\b
Hex colour
#(?:[0-9a-fA-F]{3}){1,2}\b
Trailing whitespace (flag m)
[ \t]+$
Blank lines (flag m)
^\s*$
Doubled words (flag i)
\b(\w+)\s+\1\b
Semantic version
\bv?\d+\.\d+\.\d+\b
Key = value line
^(\w+)\s*=\s*(.*)$
Keep patterns for validation loose and readable. For things like email addresses, a simple sanity check plus a confirmation step beats a 400-character expression.