The Regex Cheat Sheet: Every Symbol You Actually Need
Code Tools Studio·6 min read·
Regex syntax is dense but small: most of what you'll ever need fits in the table below. Paste any of these straight into the Regex Tester against your own test string to see exactly what matches, live.
Character classes
.— any character except a newline\d— a digit (0-9);\D— anything that isn't a digit\w— a word character (letters, digits, underscore);\W— the opposite\s— whitespace (space, tab, newline);\S— the opposite[abc]— any one ofa,b, orc;[^abc]— any character except those[a-z0-9]— a range: any lowercase letter or digit
Quantifiers
*— zero or more+— one or more?— zero or one (also marks a quantifier as "lazy" when placed after another quantifier, e.g..*?){3}— exactly 3;{2,4}— between 2 and 4;{2,}— 2 or more
Anchors
^— start of the string (or line, with themflag)$— end of the string (or line, with themflag)\b— a word boundary;\B— not a word boundary
Groups and alternation
(abc)— a capturing group, referenceable later as$1in a replacement(?:abc)— a non-capturing group: groups for the|or a quantifier without creating a$1(?<name>abc)— a named capturing group, referenced as$<name>a|b— matchesaorb
Lookahead and lookbehind
a(?=b)— matchesaonly if followed byb(positive lookahead)a(?!b)— matchesaonly if not followed byb(negative lookahead)(?<=b)a— matchesaonly if preceded byb(positive lookbehind)(?<!b)a— matchesaonly if not preceded byb(negative lookbehind)
Escaping
To match a character that's otherwise special (. * + ? ^ $ ( ) [ ] { } | \), escape it with a backslash: \. matches a literal period.
Common patterns worth keeping around
- Email (simple, practical):
^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$ - URL:
^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(\/\S*)?$ - IPv4 address:
^(\d{1,3}\.){3}\d{1,3}$(matches the shape; doesn't validate that each octet is ≤ 255) - ISO date (YYYY-MM-DD):
^\d{4}-\d{2}-\d{2}$ - Digits only:
^\d+$
None of these are exhaustively "correct" for every edge case — email validation in particular has no perfect regex — but they're the practical, good-enough versions worth reaching for first. Test any of them (or your own) against real input in the Regex Tester, which also exports a working snippet in JavaScript, Python, Java, or PHP once you've got a pattern that matches what you need.