Regex Cheatsheet: Complete Guide to Regular Expressions
2026-03-28 8 min read
Master regular expressions with this practical reference. Covers anchors, character classes, quantifiers, groups, lookaheads, and real-world pattern examples.
Regular expressions (regex) are one of the most powerful and most feared tools in a developer's toolkit. Once you understand the patterns, you can parse, validate, and transform text with a single line of code. This cheatsheet covers everything you need.
Anchors
^โ Start of string (or line in multiline mode)$โ End of string (or line in multiline mode)\bโ Word boundary\Bโ Non-word boundary
Character Classes
.โ Any character except newline\dโ Digit [0-9]\Dโ Non-digit\wโ Word character [a-zA-Z0-9_]\sโ Whitespace (space, tab, newline)[abc]โ a, b, or c[^abc]โ Not a, b, or c[a-z]โ Lowercase letter
Quantifiers
*โ 0 or more+โ 1 or more?โ 0 or 1 (also makes quantifiers lazy){n}โ Exactly n times{n,m}โ Between n and m times
Groups and Lookaheads
(abc)โ Capturing group(?:abc)โ Non-capturing group(?=abc)โ Positive lookahead(?!abc)โ Negative lookaheada|bโ a or b
Real-World Examples
- Email:
^[\w.+-]+@[\w-]+\.[a-z]{2,}$ - URL:
^https?://[^\s]+$ - Indian phone:
^[6-9]\d{9}$ - Date (YYYY-MM-DD):
^\d{4}-\d{2}-\d{2}$