Lesson 10 of 120 completed
Common Real-World Patterns
Combine all the concepts you've learned to build patterns for common real-world use cases.· 10 min
Concept
Real-world regex patterns combine anchors, character classes, quantifiers, and groups. The key is to be specific about what varies and what is fixed.
Some common patterns:
**Email (simplified):**
[\w.+\-]+@[\w\-]+\.[\w.\-]+
**US Phone:**
\(?\d{3}\)?[\s\-.\u2013]?\d{3}[\s\-.\u2013]?\d{4}
**Hex color:**
#[0-9a-fA-F]{6}(?:[0-9a-fA-F]{2})?
Building patterns step by step — fixed text first, then variable parts — makes complex patterns approachable.
/[\w.+\-]+@[\w\-]+\.[a-zA-Z]{2,}/gA simplified email pattern: local-part @ domain . tld
user@example.com
test.user+label@sub.domain.org
notanemail
@nodomain.com
/#[0-9a-fA-F]{6}/gMatches a CSS hex color code
#ff5733
#AABBCC
#gggggg
rgb(255,87,51)
Exercise
Write a pattern matching a US ZIP code: either 5 digits alone, or 5 digits + dash + 4 digits (ZIP+4 format).
Your pattern:
Must match
ZIP: 12345
90210
12345-6789
ZIP+4: 00000-9999
Must not match
1234
123456
12345-678
abcde
12345-12345