Postal codes by country — tested regexes
A postal code is almost never just ‘N digits’: excluded prefixes, leading zeros, forbidden letter pairs, allocated ranges. Each page compares a fixed regex against real attempts found online, with their real score on a test corpus.
18 countries covered.
- Argentine postal code (CPA)Argentine postal code (CPA) regex tested on 27 cases, compared to 4 real attempts: the letter O passes, and so does 'C1425!!!'.
- Australian postcodeAustralian postcode regex tested on 28 cases, compared to 4 real attempts including a Stack Overflow answer that rejects whole ranges.
- Brazilian postal code (CEP)Brazilian CEP regex tested on 22 cases, compared to 3 real attempts, one of which rejects the CEP typed without its hyphen, the most common form.
- Canadian postal codeCanadian postal code regex tested on 20 real cases, compared to 3 attempts including the question's own, which accepts a 7th character. Limits included.
- Czech postal code (PSČ)Czech PSČ regex tested on 22 cases, compared to 4 real attempts including a widely reused gist whose regex rejects every real code.
- Dutch postal codeDutch postcode regex tested on 26 cases, compared to 4 real attempts: SS, SA, SD pairs and a leading zero are mishandled almost everywhere.
- French postal codeFrench postal code regex tested on 21 real cases, compared against 3 patterns from the same Stack Overflow thread. Limits documented.
- German postal code (PLZ)German PLZ regex tested on 24 cases, compared to 4 real attempts including one that rejects Hanover and Cologne, and one from a Shopware plugin.
- Indian PIN codeIndian PIN code regex tested on 22 cases, compared to 4 real attempts including a Drupal module's that rejected Delhi (110001).
- Irish EircodeEircode regex tested on 26 cases, compared to 4 real attempts including the accepted answer, badly anchored, and validator.js's, almost wrong.
- Japanese postal codeJapanese postal code regex tested on 26 cases, compared to 3 real attempts: none accepts the 〒 prefix, and one requires the hyphen.
- Polish postal codePolish postal code regex tested on 23 cases, compared to 3 real attempts: none accepts the spaces that Poczta Polska tolerates.
- Portuguese postal codePortuguese postal code regex tested on 23 cases, compared to 3 real attempts, one where '{3}?' looks optional but changes nothing.
- Spanish postal codeSpanish postal code regex tested on 22 cases, compared to 4 real attempts including one rejecting Madrid and Barcelona because of '[00-52]'.
- Swedish postal codeSwedish postal code regex tested on 23 cases, compared to 3 real attempts: the unused areas 99 and 32 pass almost everywhere.
- Swiss postal code (PLZ/NPA)Swiss PLZ regex tested on 23 cases, compared to 4 real attempts: a '4 digits' that also accepts Liechtenstein, 9659 and 0000.
- UK postcodeUK postcode regex tested on 21 real cases, compared to 3 attempts including the UK government's own official regex, riddled with real bugs. Limits included.
- US ZIP code (ZIP and ZIP+4)ZIP code regex tested on 24 cases, compared to 4 real attempts including the 371-vote answer, looser than the official USPS rule.