I'm looking for the ultimate postal code and zip code regex. I'm looking for something that will cover most (hopefully all) of the world.
相关问题
- Improve converting string to readable urls
- Regex to match charset
- Regex subsequence matching
- Accommodate two types of quotes in a regex
- Set together letters and numbers that are ordinal
相关文章
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Allow only 2 decimal points entry to a textbox usi
- Comparing speed of non-matching regexp
- Regular expression to get URL in string swift with
- 请问如何删除之前和之后的非字母中文单字
- Lazy (ungreedy) matching multiple groups using reg
- when [:punct:] is too much [duplicate]
There are reasons beyond shipping for having an accurate postal code. Travel agencies doing tours that cross borders (Eurozone excepted of course) need this information ahead of time to give to the authorities. Often this information is entered by an agent that may or may not be familiar with such things. ANY method that can cut down on mistakes is a Good Idea™
However, writing a regex that would cover all postal codes in the world would be insane.
Please note that this is quite a hard problem, as stated by the accepted answer. I guess it didn't deter the folks at geonames.org though. They have a file a country info file, which doesn't fit whole into this answer - limit is at 30000 chars apparently. There are regexes for about 150 countries.
I extracted the bits relevant to this question here :
Hopefully I didn't make any mistake, my regex-fu is pretty weak.
This is a very simple RegEx for validating US Zipcode (not ZipCode Plus Four).
(?!([089])\1{4})\d{5}
Seems all five digit numeric are valid zipcodes except '00000', '88888' & '99999'.
I have tested this RegEx with http://regexpal.com/
SP
As others have pointed out, one regex to rule them all is unlikely. However, you can craft regular expressions for as many countries as you need using the address formatting info from the Universal Postal Union -- a little-known UN agency.
For example, here are the address formatting rules, including postal code, for a handful of countries (PDF format):
If someone is still interested in how to validate zip codes I've found a solution:
Using
Google Geocoding API
we can check validity of ZIP code having both Country code and a ZIP code itself.For example I live in Ukraine so I can check like this: https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:80380|country:UA
Or using JS API: https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering
Where
80380
is valid ZIP for Ukraine, actually every (#####) is valid.Google returns
ZERO_RESULTS
status if nothing found. OrOK
and a result if both are correct.Hope this will be helpful.
As noted elsewhere the variation around the world is huge. And even if something that matches the pattern does not mean it exists.
Then, of course, there are many places where postcodes are not used (e.g. much or Ireland).