Regex Cheat Sheet: The Patterns Every Developer Needs
Regular expressions are one of the most powerful tools in a developer's toolkit, yet they remain one of the most feared. This regex cheat sheet breaks down every regular expression pattern you need into practical, copy-paste-ready snippets. Whether you are validating an email, parsing a URL, or searching through logs, these patterns have you covered.
Bookmark this page and reach for it the next time you need a quick regex reference. And when you want to experiment, test your regex patterns with our free Regex Tester.
Basic Regex Syntax
Every regular expression is built from a handful of metacharacters. Learn these first and everything else becomes easier.
. Any single character (except newline)
^ Start of string
$ End of string
* Zero or more of the previous token
+ One or more of the previous token
? Zero or one (makes the previous token optional)
\ Escape a metacharacter (e.g. \. matches a literal dot)
| Alternation — match the expression on either side
( ) Grouping — capture a sub-expressionExample: ^Hello$matches only the exact string "Hello", while He..omatches "Hello", "Heyyo", or any five-character string starting with "He" and ending with "o".
Character Classes
Character classes let you match one character from a defined set. Square brackets define a custom class; shorthand codes cover the most common ones.
[abc] Match a, b, or c
[^abc] Match any character except a, b, or c
[a-z] Match any lowercase letter
[A-Z] Match any uppercase letter
[0-9] Match any digit
\d Digit — same as [0-9]
\D Non-digit — same as [^0-9]
\w Word character — same as [a-zA-Z0-9_]
\W Non-word — same as [^a-zA-Z0-9_]
\s Whitespace — space, tab, newline, etc.
\S Non-whitespaceQuantifiers
Quantifiers control how many times a token must appear. By default they are greedy (match as much as possible). Append ? to make them lazy (match as little as possible).
* Zero or more (greedy)
+ One or more (greedy)
? Zero or one (greedy)
{3} Exactly 3
{3,} 3 or more
{3,7} Between 3 and 7
*? Zero or more (lazy)
+? One or more (lazy)
?? Zero or one (lazy)Greedy vs. lazy matters most inside longer strings. For instance, ".+" applied to "a" and "b" matches the entire string, while ".+?" matches only "a".
Common Regex Patterns
Below are battle-tested regular expression patterns for the validations developers encounter most often. Copy them straight into your codebase or paste them into a regex tester to experiment.
Email Validation
Regex email validation is one of the most common developer tasks. This pattern covers the vast majority of real-world addresses while staying readable.
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$Matches: user@example.com, first.last@company.co.uk. Does not match strings missing the @ or domain portion.
URL Matching
https?:\/\/(www\.)?[a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_\+.~#?&\/=\-]*)Matches HTTP and HTTPS URLs including paths, query strings, and fragments. Useful for extracting links from plain text or validating user input.
Phone Number Formats
# US phone (flexible formatting)
^(\+1[\-\s]?)?\(?\d{3}\)?[\-\s]?\d{3}[\-\s]?\d{4}$
# International (E.164)
^\+[1-9]\d{1,14}$The US pattern accepts formats like (555) 123-4567, 555-123-4567, and +1 555 123 4567. The E.164 pattern validates any international number in its canonical form.
IP Address Validation
# IPv4
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
# IPv6 (simplified)
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$The IPv4 pattern ensures each octet falls between 0 and 255. The IPv6 pattern matches eight colon-separated groups of hex digits.
Date Format Patterns
# YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
# MM/DD/YYYY
^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$
# DD-Mon-YYYY (e.g. 10-Apr-2026)
^(0[1-9]|[12]\d|3[01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}$These patterns enforce valid month and day ranges structurally. For calendar-accurate validation (e.g. rejecting Feb 30), combine regex with application logic.
Lookahead and Lookbehind
Lookarounds assert that a pattern exists (or does not exist) adjacent to the current position without consuming characters. They are essential for advanced matching scenarios like password rules or context-sensitive replacements.
(?=...) Positive lookahead — next chars match ...
(?!...) Negative lookahead — next chars do NOT match ...
(?<=...) Positive lookbehind — preceding chars match ...
(?<!...) Negative lookbehind — preceding chars do NOT match ...Practical example — a password strength check requiring at least one uppercase letter, one digit, and one special character:
^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Each (?=...) lookahead verifies a condition without advancing the cursor, so all three conditions are checked independently against the full string.
Regex Flags
g Global — find all matches, not just the first
i Case-insensitive matching
m Multiline — ^ and $ match line boundaries
s Dotall — . matches newline characters
u Unicode — treat the pattern as Unicode code points
y Sticky — match only from lastIndex positionTips for Writing Efficient Regex
1. Be as specific as possible. Replace generic patterns like .* with narrower classes like [a-z]+. Specificity reduces backtracking and makes intent clear.
2. Anchor your patterns. Use ^ and $ when you intend to match the entire string. Without anchors, a pattern can match a substring you did not expect.
3. Prefer non-capturing groups. Use (?:...) instead of (...) when you do not need the captured value. This is lighter on memory and slightly faster.
4. Watch for catastrophic backtracking. Nested quantifiers like (a+)+ can cause exponential matching time. Always test edge cases with long input strings.
5. Keep patterns readable. Use verbose mode or break complex expressions into named parts. A regex nobody can read is a regex nobody can maintain.
6. Test interactively. Writing regex blind is a recipe for bugs. Test your regex patterns with our free Regex Tester to see matches highlighted in real time as you type.
Quick Reference
Pattern Purpose
─────────────────────────────────────────────
\d{3}-\d{4} US phone suffix (e.g. 555-1234)
\b\w+@\w+\.\w+\b Simple email detection in text
\$[\d,]+\.\d{2} US currency ($1,234.56)
#?[0-9a-fA-F]{6} Hex color code (#FF5733)
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b Quick IPv4 match
\d{4}-\d{2}-\d{2} ISO date (2026-04-10)Keep this regex cheat sheet handy whenever you are writing validators, parsing logs, or cleaning data. Regular expression patterns can seem dense at first glance, but broken down into their building blocks they follow a clear, logical structure.
Try Septim Forge Pro
22 developer tools, all running in your browser. Pro unlocks advanced tools for a one-time $9.
Get Pro — $9 Lifetime