Blog
Regex cheat sheet for common patterns
A quick reference for the patterns that come up constantly, without re-deriving them from scratch.
Blog
A quick reference for the patterns that come up constantly, without re-deriving them from scratch.
Most regex you'll ever write reuses a small set of building blocks. Here's the shortlist worth memorizing rather than looking up every time:
Anchors: ^ matches the start of a string (or line, with the m flag), $ matches the end. \b matches a word boundary — useful for matching whole words without also matching inside longer ones.
Character classes: \d for a digit, \w for a word character (letters, digits, underscore), \s for whitespace. Capitalized versions (\D, \W, \S) match the opposite.
Quantifiers: * means zero or more, + means one or more, ? means zero or one, and {n,m} means between n and m times. Adding ? after a quantifier makes it non-greedy — matching as little as possible instead of as much as possible.
Groups: parentheses (...) capture a match for later use; (?:...) groups without capturing, which is useful when you need grouping for a quantifier but don't need the matched text back.
A few patterns that come up often: ^\S+@\S+\.\S+$ as a loose email check, \d{4}-\d{2}-\d{2} for an ISO date, and https?:\/\/\S+ for a URL. None of these are fully rigorous (real email validation especially is messier than it looks), but they cover the common case.
The fastest way to build confidence in a pattern is to test it against real edge cases rather than just the happy path — the regex tester highlights matches live so you can see exactly what a pattern does and doesn't catch.