20 Common Regex Patterns Every Developer Should Know
Essential Regex Patterns
Here are 20 regex patterns you'll use regularly in your development work.
Validation Patterns
1. Email Address
`` [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
`
2. URL
` https?:\/\/[\w\-._~:/?#[\]@!$&'()*+,;=%]+
`
3. Phone Number (US)
` \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} Matches: (555) 123-4567, 555-123-4567, 555.123.4567 \b(?:\d{1,3}\.){3}\d{1,3}\b
`
`4. IPv4 Address
`
5. Date (YYYY-MM-DD)
` \d{4}-\d{2}-\d{2}
`
6. Time (HH:MM)
` ([01]?\d|2[0-3]):[0-5]\d
`
7. Hex Color
` #[0-9A-Fa-f]{6}\b
`
8. Credit Card (Basic)
` \d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}
`
9. ZIP Code (US)
` \d{5}(-\d{4})?
`
10. Username
` ^[a-zA-Z0-9_]{3,16}$
`
Extraction Patterns
11. Extract Domain from URL
` https?:\/\/([^/]+) Group 1 captures the domain #(\w+)
`
`12. Extract Hashtags
`
13. Extract Mentions
` @(\w+)
`
14. Extract Numbers
` -?\d+\.?\d*
`
15. Extract HTML Tags
` <([a-z]+)[^>]*>
`
Text Processing
16. Remove Extra Whitespace
` \s+ Replace with single space \b\w+\b
`
`17. Match Words Only
`
18. Match Quoted Strings
` "([^"]*)"
`
19. Match Parenthetical Content
` \(([^)]+)\)
`
20. Split by Multiple Delimiters
` [,;\s]+
``
Usage Tips