Essential Regex Patterns Reference Guide
Experienced developers don't memorize every regex pattern—they keep reference guides of commonly used patterns and adapt them for specific needs. Understanding the most useful patterns allows you to solve the majority of text processing problems without reinventing the wheel. This comprehensive reference covers the patterns you'll encounter most frequently across different programming tasks.
Validation Patterns
Email Address
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches: [email protected], [email protected] Doesn't Match: invalid@, @example.com, user@domain
Simpler Version (less strict):
^[^\s@]+@[^\s@]+\.[^\s@]+$
Phone Number (US Format)
^(\+1)?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
Matches: 555-123-4567, (555) 123-4567, +1 555 123 4567, 5551234567 Doesn't Match: 55512, 123456789
International Phone
^\+?[1-9]\d{1,14}$
Matches: +14155552671, 14155552671, +33123456789 Format: E.164 standard format
Password Validation
Strong password (8+ chars, uppercase, lowercase, number):
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
Matches: Password1, SecurePass123, MyP@ssw0rd Doesn't Match: password, PASSWORD, Pass1, weak
Very Strong (with special characters):
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Username Validation
Alphanumeric and underscore, 3-20 characters:
^[a-zA-Z0-9_]{3,20}$
Matches: john_doe, user123, my_name Doesn't Match: ab, user-name, user@name
URL Validation
^https?://(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)$
Simpler Version:
^https?://[^\s/$.?#].[^\s]*$
Matches: http://example.com, https://www.example.com/page?id=123 Doesn't Match: ftp://example.com, htp://example.com, example.com
IPv4 Address
^(?:(?: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]?)$
Matches: 192.168.1.1, 255.255.255.255, 0.0.0.0 Doesn't Match: 256.1.1.1, 192.168.1, 192.168.1.1.1
Simpler Version (less strict):
^(\d{1,3}\.){3}\d{1,3}$
Credit Card Number
^[0-9]{13,19}$
With Formatting (spaces or dashes):
^[0-9]{4}[\s-]?[0-9]{4}[\s-]?[0-9]{4}[\s-]?[0-9]{4}$
Matches: 1234567890123456, 4532-1234-5678-9010, 4532 1234 5678 9010 Note: Doesn't validate checksum, only format
Date Formats
YYYY-MM-DD:
^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$
MM/DD/YYYY:
^(?:0[1-9]|1[0-2])/(?:0[1-9]|[12][0-9]|3[01])/[0-9]{4}$
DD/MM/YYYY:
^(?:0[1-9]|[12][0-9]|3[01])/(?:0[1-9]|1[0-2])/[0-9]{4}$
Time Formats
HH:MM:SS (24-hour):
^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$
HH:MM AM/PM (12-hour):
^(?:0\d|1[0-2]):[0-5]\d\s(?:AM|PM|am|pm)$
String Matching and Extraction Patterns
Entire String is Digits
^\d+$
Entire String is Letters Only
^[a-zA-Z]+$
Entire String is Alphanumeric
^[a-zA-Z0-9]+$
Find All Numbers
\d+
Find All Words
\b\w+\b
Match HTML Tags
<([a-z]+)([^>]*)>(.*?)</\1>
Matches: <div class="test">content</div>, <span>text</span>
Note: Better to use HTML parser for complex HTML
Find Capitalized Words
\b[A-Z][a-z]*\b
Find CamelCase Words
\b[a-z]+(?:[A-Z][a-z]+)*\b
Find snake_case Words
[a-z]+(?:_[a-z]+)*
Extraction Patterns
Extract Domain from Email
@(.+)$
Use capture group 1 for domain
Extract Domain from URL
(?:https?:\/\/)?(?:www\.)?([^\/?]+)
Extract Numbers from Mixed Text
\d+\.?\d*
Extracts: 123, 45.67, 89
Extract Words and Phrases
\b\w+(?:\s+\w+)*\b
Replacement Patterns
Convert Date Format (YYYY-MM-DD to MM/DD/YYYY)
Find: (\d{4})-(\d{2})-(\d{2})
Replace: $2/$3/$1
Example: 2024-01-15 → 01/15/2024
Convert Email to Username
Find: ^([^@]+)@.+$
Replace: $1
Example: [email protected] → john.doe
Remove Multiple Spaces
Find: \s+
Replace: (single space)
Example: "word1 word2" → "word1 word2"
Convert Underscores to Spaces
Find: _
Replace: (space)
Example: hello_world → hello world
Remove All Numbers
Find: \d+
Replace: (nothing)
Example: Item123 costs $45.99 → Item costs $..
Advanced Patterns
Lookahead - Match If Followed By
Match "price" only if followed by ":":
price(?=:)
Lookbehind - Match If Preceded By
Match "amount" only if preceded by "$":
(?<=\$)amount
Non-Capturing Group
Group without creating capture:
(?:cat|dog|bird)
Named Capture Groups
For complex patterns with multiple captures:
(?<area>\d{3})-(?<exchange>\d{3})-(?<line>\d{4})
Alternation - Match Any Option
cat|dog|bird
Matches any of the three words
Word Boundaries
\bexact\b
Matches "exact" but not in "exactly" or "inexact"
Language-Specific Patterns
Phone Number (with Extensions)
^(\+\d{1,2}\s?)?\(?(\d{3})\)?[\s.-]?(\d{3})[\s.-]?(\d{4})(?:\s?ext\.?\s?(\d+))?$
Social Security Number (US)
^\d{3}-\d{2}-\d{4}$
ISBN (10 or 13 digit)
^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$
(Complex! Better to use libraries for ISBN validation)
UPC Code (12 digits)
^\d{12}$
Color Hex Code
^#(?:[0-9a-fA-F]{3}){1,2}$
Matches: #fff, #ffffff, #ABC, #AABBCC
Common Mistakes to Avoid
Missing Escape for Special Characters
WRONG: ^test.txt$ (. matches any character)
RIGHT: ^test\.txt$ (escaped dot for literal dot)
Greedy When You Need Lazy
WRONG: <.*> (matches too much)
RIGHT: <.*?> (matches minimal)
Forgetting Anchors
WRONG: \d+ (matches any number anywhere)
RIGHT: ^\d+$ (entire string is numbers)
Not Handling Edge Cases
Test with: empty strings, null, spaces, special characters, very long strings
Testing Tools
Always test patterns in these tools:
- regex101.com: Best for learning and testing
- regexr.com: Visual debugging
- Your language's REPL: Test in your actual environment
Quick Reference by Task
| Task | Pattern |
|---|---|
| Email validation | ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ |
| Phone (US) | ^\d{3}-\d{3}-\d{4}$ |
| URL | ^https?://[^\s/$.?#].[^\s]*$ |
| Password strong | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ |
| Username | ^[a-zA-Z0-9_]{3,20}$ |
| IPv4 | ^(\d{1,3}\.){3}\d{1,3}$ |
| Date YYYY-MM-DD | ^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$ |
| All digits | ^\d+$ |
| All letters | ^[a-zA-Z]+$ |
| All alphanumeric | ^[a-zA-Z0-9]+$ |
Conclusion
These patterns form the foundation of most regex usage in real-world applications. Rather than memorizing them, keep this guide handy for reference, and gradually internalize the patterns you use most frequently. Always test patterns thoroughly with edge cases before deploying to production. As you gain experience, you'll recognize patterns and be able to adapt them quickly for your specific needs.

