Regex Cheat Sheet
Quick Reference for Developers
Every pattern, quantifier, anchor, group and flag — with examples you can copy and test immediately.
Reference·Updated May 2026
Regex cheat sheet for developers: a single-page reference covering everything from basic character classes to lookaheads, with copy-paste patterns for the most common use cases. Each section links to the free regex tester so you can try any pattern instantly.
Character Classes
| Pattern | Meaning |
|---|---|
| . | Any character except newline (add s flag for newline too) |
| \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, carriage return |
| \S | Non-whitespace |
| [abc] | Character set — matches a, b, or c |
| [^abc] | Negated set — matches anything except a, b, c |
| [a-z] | Range — matches any lowercase letter |
| [a-zA-Z0-9] | Multiple ranges — alphanumeric |
Anchors
| Pattern | Meaning |
|---|---|
| ^ | Start of string (or start of line with m flag) |
| $ | End of string (or end of line with m flag) |
| \b | Word boundary — between \w and \W |
| \B | Non-word boundary |
| \A | Start of string (not affected by m flag) — Python/Ruby |
| \Z | End of string (not affected by m flag) — Python/Ruby |
Quantifiers
| Pattern | Meaning |
|---|---|
| * | Zero or more — greedy |
| + | One or more — greedy |
| ? | Zero or one (makes preceding element optional) |
| {n} | Exactly n times |
| {n,} | n or more times |
| {n,m} | Between n and m times (inclusive) |
| *? | Zero or more — lazy (matches as few as possible) |
| +? | One or more — lazy |
| ?? | Zero or one — lazy |
Groups & References
| Pattern | Meaning |
|---|---|
| (abc) | Capture group — stores match, referenced as $1 or \1 |
| (?:abc) | Non-capturing group — groups without storing |
| (?P<name>abc) | Named capture group — referenced as (?P=name) |
| \1 | Backreference to first capture group |
| (?|...) | Branch reset group (PCRE only) |
| a|b | Alternation — matches a or b |
Lookarounds
| Pattern | Meaning |
|---|---|
| (?=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 / Modifiers
| Pattern | Meaning |
|---|---|
| g | Global — find all matches, not just first |
| i | Case-insensitive |
| m | Multiline — ^ and $ match start/end of each line |
| s | Dotall — makes . match newlines too |
| u | Unicode — enables full Unicode matching |
| x | Extended — allows whitespace and comments in pattern |
Escape Sequences
| Pattern | Meaning |
|---|---|
| \. | Literal dot (escape any metacharacter: * + ? ^ $ { } [ ] | ( ) \) |
| \n | Newline |
| \t | Tab |
| \r | Carriage return |
| \0 | Null character |
| \uFFFF | Unicode character by code point |
| \xFF | Character by hex code |
Common Ready-to-Use Patterns
Copy any of these directly into your code or into the regex tester to verify against your own input.
Email (practical)
/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/
Covers the vast majority of valid addresses. See the full guide on email regex for edge cases.
URL (http/https)
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&\/=]*)/
Matches http and https URLs with optional www and path.
IPv4 address
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/
Validates each octet is 0–255.
Date (YYYY-MM-DD)
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
ISO 8601 date format validation.
Phone (US)
/^\+?1?\s?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$/
Handles most US phone number formats.
Hex color
/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
Matches 3 or 6 digit hex colors, with or without #.
UUID v4
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
Validates UUID v4 format.
JWT token
/^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]*$/
Three base64url segments separated by dots.
Slug (URL-safe)
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Lowercase alphanumeric with hyphens, no leading/trailing hyphen.
IP + port
/^(\d{1,3}\.){3}\d{1,3}:\d{1,5}$/
Quick match for host:port combinations.
Test Any Pattern Instantly
Paste any regex from this cheat sheet and test it against your own input — all in your browser.
Open Regex Tester →