最终的邮政编码和邮政编码正则表达式是什么?
我正在寻找最终的邮政编码和邮政编码正则表达式。 我正在寻找能够覆盖世界大部分(希望是全部)的东西。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在寻找最终的邮政编码和邮政编码正则表达式。 我正在寻找能够覆盖世界大部分(希望是全部)的东西。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(21)
unicode CLDR 包含每个国家/地区的邮政编码正则表达式。 (总共 158 个正则表达式!)
core.zip
/unicode.org/Public/cldr/26.0.1/common/supplemental/postalCodeData.xml
(直接内容:common/supplemental/postalCodeData.xml)Google 还提供了一项网络服务,其中包含各个国家/地区的地址格式信息(包括邮政编码) - http://i18napis .appspot.com/地址
(我通过 http://unicode.org/review/pri180/ 找到了该链接)
编辑
这里有一份 postalCodeData.xml 正则表达式:
The unicode CLDR contains the postal code regex for each country. (158 regex's in total!)
core.zip
from http://unicode.org/Public/cldr/26.0.1/common/supplemental/postalCodeData.xml
from the unzipped content (direct content: common/supplemental/postalCodeData.xml)Google also has a web service with per-country address formatting information, including postal codes, here - http://i18napis.appspot.com/address
(I found that link via http://unicode.org/review/pri180/ )
Edit
Here a copy of postalCodeData.xml regex :
空无一人。
世界各地的邮政编码并不遵循共同的模式。 在某些国家/地区,它们由数字组成,在其他国家/地区,它们可以是数字和字母的组合,有些可以包含空格,有些可以包含点,字符数可以从两个到至少六个不等...
您可以做什么(理论上) )是为世界上每个国家创建一个单独的正则表达式,IMO 不推荐。 但您仍然会缺少验证部分:邮政编码
12345
可能存在,但12346
不存在,也许12344
也不存在。 如何使用正则表达式检查这一点?你不能。
There is none.
Postal/zip codes around the world don't follow a common pattern. In some countries they are made up by numbers, in others they can be combinations of numbers an letters, some can contain spaces, others dots, the number of characters can vary from two to at least six...
What you could do (theoretically) is create a seperate regex for every country in the world, not recommendable IMO. But you would still be missing on the validation part: Zip code
12345
may exist, but12346
not, maybe12344
doesn't exist either. How do you check for that with a regex?You can't.
使用这些正则表达式
use these regx
每个邮政编码系统仅使用 AZ 和/或 0-9,有时使用空格/破折号
并非每个国家/地区都使用邮政编码(例如,都柏林以外的爱尔兰),但我们在这里将忽略它。
最短的邮政编码格式是塞拉利昂,带有
NN
最长的是美属萨摩亚,带有
NNNNN-NNNNNN
您应该留出一个空格或破折号。
不应以空格或破折号开头或结尾
这应涵盖以上内容:
Every postal code system uses only A-Z and/or 0-9 and sometimes space/dash
Not every country uses postal codes (ex. Ireland outside of Dublin), but we'll ignore that here.
The shortest postal code format is Sierra Leone with
NN
The longest is American Samoa with
NNNNN-NNNNNN
You should allow one space or dash.
Should not begin or end with space or dash
This should cover the above:
试图用一个正则表达式覆盖整个世界是完全不可能的,当然也是不可行或不推荐的。
不是自吹自擂,但我写了一些非常详尽的正则表达式,您可能会发现它们很有帮助。
加拿大邮政编码
美国邮政编码
<前><代码>^[0-9]{5}(-[0-9]{4})?$
英国邮政编码
<预><代码>^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0 -9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\ [0-9][ABD-HJLNP-UW-Z]{2}| (GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL| TDCU|TKCA)\ 1ZZ))$
不可能保证准确性,而无需实际将东西邮寄到某个地址并让人们在收到邮件时通知您,但我们可以通过消除我们知道的不良情况来缩小范围。
Trying to cover the whole world with one regular expression is not completely possible, and certainly not feasible or recommended.
Not to toot my own horn, but I've written some pretty thorough regular expressions which you may find helpful.
Canadian postal codes
US ZIP codes
UK post codes
It is not possible to guarantee accuracy without actually mailing something to an address and having the person let you know when they receive it, but we can narrow things by down by eliminating cases that we know are bad.
我们使用以下内容:
加拿大
美国
其他
We use the following:
Canada
America
Other
如果有人仍然对如何验证邮政编码感兴趣,我找到了一个解决方案:
使用
Google Geocoding API
我们可以检查同时具有国家/地区代码 和 <邮政编码本身。例如,我住在乌克兰,所以我可以这样检查:
https://maps.googleapis.com/maps /api/geocode/json?components=postal_code:80380|country:UA
或使用 JS API:https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering
其中
80380
是乌克兰的有效邮政编码,实际上每个 (### ##) 已验证。如果未找到任何内容,Google 将返回
ZERO_RESULTS
状态。或者
OK
,如果两者都正确,则给出结果。希望这会有所帮助。
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.Or
OK
and a result if both are correct.Hope this will be helpful.
根据您的应用程序,您可能希望对大多数访问者的来源国家/地区实施正则表达式匹配,而对其余访问者不进行验证(接受任何内容)。
Depending on your application, you might want to implement regex matching for the countries where most of your visitors originate and no validation for the rest (accept anything).
请注意,正如已接受的答案所述,这是一个相当困难的问题。
我想这并没有阻止 geonames.org 的人们。
他们有一个文件 国家/地区信息文件,该文件不完全适合此内容答案 - 显然限制为 30000 个字符。 大约有 150 个国家/地区的正则表达式。
我在这里提取了与这个问题相关的部分:
希望我没有犯任何错误,我的正则表达式非常弱。
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.
Big Jump 忘记了换行符、空格和控制字符。
国际邮政编码是一种停滞问题。
Big Jump forgot about line breaks, blanks and control characters.
International postal codes are a kind of halting problem.
正如其他人指出的那样,用一个正则表达式来统治所有这些是不可能的。 但是,您可以使用 来自万国邮政联盟的地址格式信息——一个鲜为人知的联合国机构。
例如,以下是少数国家/地区的地址格式规则,包括邮政编码(PDF 格式):
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):
问题在于,您可能没有好的方法来跟上地球另一端国家/地区不断变化的邮政编码要求,并且您没有共同语言。 除非您有足够大的预算来跟踪这一点,否则您最好将验证地址的责任交给谷歌或雅虎。
两家公司都通过可编程 API 提供地址查找功能。
The problem is going to be that you probably have no good means of keeping up with the changing postal code requirements of countries on the other side of the globe and which you share no common languages. Unless you have a large enough budget to track this, you are almost certainly better off giving the responsibility of validating addresses to google or yahoo.
Both companies provide address lookup facuilities through a programmable API.
鉴于每个国家/地区都有如此多的边缘情况(例如,伦敦地址可能使用与英国其他地区略有不同的格式),我认为除了可能之外,没有最终的正则表达式:
最好使用相当的 正则表达式广泛的模式(并不像上面那么广泛),或者用自己的特定模式对待每个国家/地区!
更新:但是,可以根据许多较小的、特定于区域的规则动态构建正则表达式 - 但不确定性能!
许多特定于国家/地区的模式可以在 RegExLib 网站上找到。
Given that there are so many edge cases for each country (eg. London addresses may use a slightly different format to the rest of the UK) I don't think that there is an ultimate regex other than maybe:
Best of going with a fairly broad pattern (well not quite as broad as the above), or treat each country/region with a specific pattern of its own!
UPDATE: However, it may be possible to dynamically construct a regex based upon lots of smaller, region specific rules - not sure about performance though!
Lots of country specific patterns can be found on the RegExLib site.
你为什么要这样做以及你为什么关心? 正如汤姆·里特(Tom Ritter)指出的那样,除非您确实要向该地址发送一些东西,否则您是否有邮政编码并不重要,更不用说它是否有效了。 即使您预计有一天会给他们寄一些东西,并不意味着您今天需要邮政编码。
Why are you doing this and why do you care? As Tom Ritter pointed out, it doesn't matter whether you even have a ZIP/postal code at all, much less whether it's valid or not, until and unless you are actually going to be sending something to that address. Even if you expect that you will be sending them something someday, that doesn't mean you need a postal code today.
正如其他地方所指出的,世界各地的差异是巨大的。 即使某些东西与模式匹配并不意味着它存在。
当然,还有很多地方不使用邮政编码(例如很多地方或爱尔兰)。
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).
除了运输之外,还有其他原因需要提供准确的邮政编码。 进行跨境旅游(当然欧元区除外)的旅行社需要提前向当局提供这些信息。 通常,此信息是由可能熟悉或不熟悉此类事物的代理输入的。 任何可以减少错误的方法都是一个好主意™
但是,编写一个涵盖世界上所有邮政编码的正则表达式将是疯狂的。
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.
有人询问格式化邮寄地址的列表,我认为这就是他正在寻找的......
弗兰克的邮政地址强制指南:
我的工作使用了一些工具来协助完成此任务:
- Lexis-Nexis 服务,包括 NCOA 查找(您将“免费”获得地址标准化)
- “梅丽莎数据”http://www.melissadata.com
Somebody was asking about list of formatting mailing addresses, and I think this is what he was looking for...
Frank's Compulsive Guide to Postal Addresses: http://www.columbia.edu/~fdc/postal/
Doesn't help much with street-level issues, however.
My work uses a couple of tools to assist with this:
- Lexis-Nexis services, including NCOA lookups (you'll get address standardization for "free")
- "Melissa Data" http://www.melissadata.com
查看 WooCommerce 邮政编码验证功能。 即使您在项目中根本不使用 Woo/WordPress/PHP,也是相关的。
https:// github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-validation.php#L47-L125
将在此处发布 2024 年的当前代码,因为最好的做法是不要依赖链接...但实际上 WC 项目维护得很好,因此您将来可能应该获取最新的更新版本。
Checkout the WooCommerce postcode validation function. Relevant even if you're not using Woo/WordPress/PHP at all in your project.
https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-validation.php#L47-L125
Going to post the current code from 2024 here as it's best practice on SO not to rely on links... but really the WC project is well maintained so you should probably grab the latest updated version there in the future.
这是一个非常简单的正则表达式,用于验证美国邮政编码(不是邮政编码加四):
似乎所有五位数字都是有效的邮政编码,除了
00000
、88888
和 。99999
。我已经用 http://regexpal.com/
SP测试了这个正则表达式
This is a very simple RegEx for validating US Zipcode (not ZipCode Plus Four):
Seems all five digit numeric are valid zipcodes except
00000
,88888
&99999
.I have tested this RegEx with http://regexpal.com/
SP
我知道这是一个老问题,但我偶然发现了同样的问题。
我有来自 100 多个国家/地区的发票,并且正在尝试通过邮政编码找到正确的债权人(如果其他所有检查都失败)。
所以我所做的是编写一个简短的Python脚本,它从字符串创建一个模式:
这样我就为我们历史上所有的zip(按国家/地区)创建了所有不同的可能的正则表达式,并将它们写回到数据库表中(即类似于这到底是:
COUNTRY:RE PATTERN:(\d{5})\b
[这可能是哪个国家/地区;D])也许它对某人有帮助。
I know this is an old quesiton, but I stumbled across the same problem.
I have invoices from over 100 countries and am trying to get the correkt creditor over the zip (if every other check is failing).
So what I did is writing a short Python Script, that creates a pattern from a string:
With that I created all the different possible regexes for all zips (by country) we have historically and wrote them back into a db table (i.e. something like this in the end:
COUNTRY:RE PATTERN:(\d{5})\b
[what ever country this might be ;D])Maybe it helps someone.
如果
邮政编码
允许字符和数字(字母数字),则在匹配的地方将使用以下正则表达式,5或9或10个字母数字字符和一个连字符(-
):If
Zip Code
allows characters and digits (alphanumeric), below regex would be used where it matches, 5 or 9 or 10 alphanumeric characters with one hypen (-
):