Real-world validators
Match the formats you meet every day — integers, decimals, hex colours, times, dates, emails, slugs, IPs, ZIPs and more.
Read the rules: Common patterns in the Guide →Drill it: Format validators →
Builds on Lookarounds
What you'll learn
Steps · 14
- 01Validating the real worldBriefing
- 02Whole numbersHunt
- 03Decimal numbersHunt
- 04Hex coloursHunt
- 0524-hour timeHunt
- 06ISO datesHunt
- 07Simple emailsHunt
- 08URL slugsHunt
- 09IPv4 addressesHunt
- 10US ZIP codesHunt
- 11HashtagsHunt
- 12UsernamesHunt
- 13Golf: short hexHunt
- 14Validators completeBriefing
Regex validation patterns
A validation regex answers one question: does this text have the right shape? Sign-up forms, spreadsheets and config files are full of values that should look a certain way, such as a time, a date, a colour code or an email address. Wrap the pattern in ^ and $ and it must match the whole value, so stray spaces or extra characters fail the check.
Each format is built from small parts. \d is a digit, {4} asks for exactly four, ? makes a part optional, and a group such as (am|pm) lists the allowed choices. Number ranges need the most care: (100|[1-9]?\d) covers 0 to 100, while \d+ would also let 250 through. In each hunt on this track you write one validator that catches the good values and spares the near-misses.
Be honest about what a regex can know. It checks format, not meaning. A date pattern happily accepts 31/02/2026 unless you write extra branches for each month, and an email pattern can't tell whether the inbox exists. Use the regex as a fast first filter, then let real code check the calendar, send a confirmation email or look up the record.
Examples
Catches
- 9:30 am
- 12:05PM
- 07:45 pm
Spares
- 13:00 pm
- 9:60 am
- 9:30
A 12-hour time: hours 1 to 12 with an optional leading zero, and the
iflag accepts bothpmandPM.Catches
- 05/11/2026
- 31/12/1999
- 31/02/2026
Spares
- 5/11/2026
- 12/13/2026
- 32/01/2026
Day and month are range-checked, yet
31/02/2026still passes, because the pattern knows shapes, not calendars.Catches
- ABC-1234
- XYZ-0042
Spares
- AB-1234
- abc-1234
- ABC-12345
- ABC1234
A product code: exactly three capital letters, a dash and four digits, with the anchors refusing anything longer.
Catches
- 0%
- 42%
- 100%
Spares
- 101%
- 07%
- 42
[1-9]?\dcovers 0 to 99 without a leading zero, and100gets a branch of its own.
Common mistakes
- Forgetting the anchors. Without
^and$, a pattern for four digits also passes12345andabc2026, because it finds a match somewhere inside. - Leaving the dot bare. In a decimal or a domain,
.matches any character, so3x14slips through a number check; write\.when you mean a real dot. - Chasing every edge case in one pattern. A fully standard email regex runs to hundreds of characters; a short pattern plus a confirmation email is easier to read and catches more real mistakes.
Read the rules: Common patterns in the Guide →Drill it: Format validators →
Read more: Email regex: copy-ready patterns that actually work →
Read more: Regex for IPv4 addresses: matching 0 to 255 properly →