Regex Cheat Sheet
Regular expression syntax quick reference
Anchors
Command / Pattern Description
^abc start of string/line
abc$ end of string/line
\b word boundary
\B not a word boundary
Character Classes
Command / Pattern Description
. any character except newline
\d \D digit / non-digit
\w \W word char / non-word char
\s \S whitespace / non-whitespace
[abc] a, b, or c
[^abc] not a, b, or c
[a-z0-9] range of characters
Quantifiers
Command / Pattern Description
a* 0 or more
a+ 1 or more
a? 0 or 1 (optional)
a{3} exactly 3
a{2,4} between 2 and 4
a{2,} 2 or more
a*? (lazy) non-greedy match
Groups & Alternation
Command / Pattern Description
(abc) capturing group
(?:abc) non-capturing group
(?P<name>abc) named group
a|b a or b
\1 backreference to group 1
Lookaround
Command / Pattern Description
(?=abc) positive lookahead
(?!abc) negative lookahead
(?<=abc) positive lookbehind
(?<!abc) negative lookbehind
Common Patterns
Command / Pattern Description
^[\w.-]+@[\w.-]+\.\w+$ basic email
^\d{3}-\d{3}-\d{4}$ phone number (US)
https?://\S+ URL
^\d{4}-\d{2}-\d{2}$ date YYYY-MM-DD
^[A-Za-z0-9_]{3,16}$ username, 3-16 chars
Python re Module
Command / Pattern Description
[Link](p, s) match at start of string
[Link](p, s) find first match anywhere
[Link](p, s) all non-overlapping matches
[Link](p, repl, s) replace matches
[Link](p, s) split string by pattern
[Link](p) precompile pattern for reuse