Regular expressions are one of the most powerful text-processing tools in a developer's toolkit — and one of the most misunderstood. This cheat sheet covers the syntax you'll use 90% of the time, plus ready-to-copy patterns for common validation tasks.
Core Syntax Quick Reference
Character Classes
. Any character except newline
\d Digit [0-9]
\D Non-digit [^0-9]
\w Word character [a-zA-Z0-9_]
\W Non-word character
\s Whitespace (space, tab, newline)
\S Non-whitespace
[abc] Any of a, b, or c
[^abc] Not a, b, or c
[a-z] Range: a through z
Quantifiers
* 0 or more
+ 1 or more
? 0 or 1 (optional)
{3} Exactly 3
{2,5} Between 2 and 5
{3,} 3 or more
Anchors & Boundaries
^ Start of string (or line with m flag)
$ End of string (or line with m flag)
\b Word boundary
\B Not a word boundary
Groups & Alternation
(abc) Capture group
(?:abc) Non-capturing group
(?<name>) Named capture group
a|b a or b (alternation)
\1 Back-reference to group 1
Lookahead & Lookbehind
(?=abc) Positive lookahead (followed by abc)
(?!abc) Negative lookahead (NOT followed by abc)
(?<=abc) Positive lookbehind (preceded by abc)
(?<!abc) Negative lookbehind (NOT preceded by abc)
Flags
g Global — find all matches, not just the first
i Case-insensitive
m Multiline — ^ and $ match line starts/ends
s Dotall — . matches newline characters too
u Unicode support
Practical Patterns
Email Address (Simple)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches most common email formats. For production validation, prefer your framework's built-in email validator or actually send a confirmation email — regex alone can't verify deliverability.
URL
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$
Matches HTTP and HTTPS URLs with optional www, path, query string, and fragment.
IPv4 Address
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Validates each octet is 0–255. Rejects 999.999.999.999 unlike a naive \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} pattern.
IPv6 Address (Simplified)
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Matches full-form IPv6. Compressed forms (with ::) require a more complex pattern.
Date: YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Validates ISO 8601 date format. Note: this doesn't catch invalid dates like Feb 31 — use a date library for that.
Time: HH:MM (24-hour)
^([01]\d|2[0-3]):[0-5]\d$
Phone Number (US)
^(\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Matches formats like (555) 123-4567, 555.123.4567, +1-555-123-4567.
Hex Color Code
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Matches both shorthand (#fff) and full (#ffffff) hex colors.
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires at least 8 characters with one lowercase, one uppercase, one digit, and one special character. Uses lookaheads to check each requirement independently.
HTML Tags
<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>
Matches opening and closing HTML tags with content. The \1 back-reference ensures the closing tag matches the opening tag.
Whitespace Trimming
^\s+|\s+$
Matches leading and trailing whitespace. Useful for cleanup, though most languages have a built-in trim() method.
Duplicate Words
\b(\w+)\s+\1\b
Finds repeated words like "the the" or "is is". Great for proofreading.
Extract Numbers from Text
-?\d+\.?\d*
Matches integers and decimals, including negative numbers.
Regex Performance Tips
- Be specific —
[a-z]+is faster than.+because the engine doesn't need to try every possible character. - Avoid catastrophic backtracking — Patterns like
(a+)+can take exponential time on certain inputs. Use atomic groups or possessive quantifiers when available. - Use non-capturing groups —
(?:abc)is slightly faster than(abc)when you don't need to reference the match. - Anchor when possible —
^pattern$tells the engine it only needs to check from the start, avoiding unnecessary scanning. - Compile once, use many times — In languages like Python (
re.compile) or Java (Pattern.compile), compile your regex once and reuse it.
Try It Yourself
Test any of these patterns with live highlighting using the Regex Tester — paste your pattern and test string, and see matches instantly in your browser.