Composing primitives into real patterns
You now have enough tools to write surprisingly powerful patterns. Without knowing quantifiers *+? from Track 3, you can already do this with just {n} from earlier hints and the classes you've learned.
A US phone number
Shape: 3 digits, hyphen, 3 digits, hyphen, 4 digits. Pattern: \d{3}-\d{3}-\d{4}. Anchor with ^...$ for whole-string validation, leave bare for searching within text.
An ISO date
Shape: 4 digits, hyphen, 2 digits, hyphen, 2 digits. Pattern: \d{4}-\d{2}-\d{2}. Same composition principle.
Notice: this matches 9999-99-99. Regex validates SHAPE, not semantics. If you need to confirm the date is real, use a date library after the regex match.
A simple email (shape only)
Real email regex is famously cursed (the longest published one is over 6,000 characters). For 99% of cases, this is enough: [\w.+-]+@[\w-]+\.[\w.-]+
Read it left to right: word characters or .+- (local part), literal @, word characters or - (domain), literal dot, word characters or .- (TLD allowing subdomains).
The pattern this teaches
Real-world regex is built by:
- Describing the shape in English ("3 digits, hyphen, 3 digits, hyphen, 4 digits").
- Replacing each piece with the smallest character class or shorthand that fits (
\d{3}, then-literal). - Anchoring if you're validating, leaving open if you're searching.
- Testing with three inputs: a known-good, a known-bad, and an edge case.
You'll do this dozens of times in Tracks 6 and 7 with quantifiers + groups added. The mental motion is the same.