regex pattern · ready to copy
Regex for matching US ZIP codes (5-digit + ZIP+4)
Match both `12345` and `12345-6789` formats. Doesn't enforce real ZIP existence.
intermediate
javascript / pcre / python4 use cases
The pattern
\b\d{5}(?:-\d{4})?\b
Test cases
| Input | Result |
|---|---|
| 94103 | ✓matches |
| 10001 | ✓matches |
| 94103-2412 | ✓matches |
| 20500-0001 | ✓matches |
| 1234 (4 digits) | ✗rejects |
| 94103-12 (4-digit suffix needed) | ✗rejects |
| abc-de | ✗rejects |
Edge cases & caveats
Will match arbitrary 5-digit numbers in text (phone area codes, etc.). Always pair with context (prior `ZIP`, `postal`, address line). For real validation, use USPS database lookup.
Common use cases
- address parsing
- shipping form validation
- geo-data normalization
- tax-rate lookup
Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab
Related
jwt · url · iso 8601 date · semver version · ↗ generate test addresses