Regular Expressions Cheat Sheet for Developers
A practical reference guide to regular expressions with common patterns, syntax, and real-world examples.
Regular expressions (regex) are powerful patterns for matching, searching, and manipulating text. This cheat sheet covers the most commonly used regex features with practical examples.
Basic Syntax
|---------|-------------|---------|
.a.c matches "abc", "a1c"\d\d{3} matches "123"\w\w+ matches "hello_123"\s\s+ matches spaces, tabs^^Hello matches "Hello world"$world$ matches "Hello world"Quantifiers
|---------|-------------|
*+?{n}{n,m}{n,}Common Patterns
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URL Matching
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Phone Number (US)
^\+?1?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
IP Address (IPv4)
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
Password Strength
At least 8 chars, one uppercase, one lowercase, one digit, one special:
^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{8,}$
Groups and Lookaheads
Capturing Groups
(\d{4})-(\d{2})-(\d{2})
// Captures year, month, day separately
Non-Capturing Groups
(?:https?|ftp)://
// Groups but doesn't capture
Lookahead and Lookbehind
(?=.*\d) // Positive lookahead: followed by digit
(?!.*\d) // Negative lookahead: NOT followed by digit
(?<=\$)\d+ // Positive lookbehind: preceded by $
(?
Flags
|------|-------------|
gimsuPerformance Tips
1. Be specific: [0-9] is faster than .*\d
2. Avoid catastrophic backtracking: Use atomic groups or possessive quantifiers
3. Anchor when possible: ^pattern$ is faster than unanchored pattern
4. Prefer character classes: [aeiou] over (a|e|i|o|u)
Test your regular expressions with our Regex Tester tool.