最终的邮政编码和邮政正则表达式是什么?

我正在寻找最终的邮政编码和邮政编码正则表达式。我正在寻找一种能覆盖世界大部分地区(希望是所有地区)的东西。

246470 次浏览

考虑到每个国家都有如此多的边缘情况(例如。伦敦地址可能使用与英国其他地区略有不同的格式)我不认为有一个终极正则表达式,除了可能:

[0-9a-zA-Z]+

最好是使用一个相当广泛的模式(好吧,不像上面那么广泛),或者用自己的特定模式对待每个国家/地区!

然而,基于许多更小的、特定于区域的规则,动态地构造一个正则表达式是可能的——但不确定性能!

RegExLib网站上可以找到许多特定国家的模式。

没有。

世界各地的邮政/邮政编码并不遵循一个共同的模式。在一些国家,它们由数字组成,在其他国家,它们可以是数字和字母的组合,有些可以包含空格,有些可以包含点,字符的数量从2个到至少6个不等……

你能做的(理论上)是为世界上每个国家创建一个单独的正则表达式,我不推荐。但是你仍然会忽略验证部分:邮政编码12345可能存在,但12346不存在,可能12344也不存在。你如何用正则表达式检查它?

你不能。

根据您的应用程序,您可能希望对大多数访问者来自的国家实现正则表达式匹配,而对其余的国家不进行验证(接受任何东西)。

你为什么要这么做,你为什么这么在乎?正如Tom Ritter所指出的,你是否有一个ZIP/邮政编码并不重要,更不用说它是否有效了,除非你真的要把东西发送到那个地址。即使你希望给他们发送一些总有一天的东西,这并不意味着你需要一个邮政编码今天

正如其他地方指出的那样,世界各地的差异很大。即使某些东西符合模式也不意味着它存在。

当然,还有很多地方不使用邮政编码(例如much或爱尔兰)。

我们使用以下方法:

加拿大

([A-Z]{1}[0-9]{1}){3}   //We raise to upper first

美国

[0-9]{5}                //-or-
[0-9]{5}-[0-9]{4}       //10 digit zip

其他

接受现状

试图用一个正则表达式覆盖整个世界是不完全可能的,当然也不可行,也不推荐。

不是自吹自擂,但我已经写了一些非常全面的正则表达式,你可能会觉得有用。

  • < p > 加拿大邮政编码

    Basic validation:
    ^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPR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K-NPRSTVWXZ]|5[ABCEGHJ-NPRSTV-Z]|6[ABCEGHJ-NPRSTV-Z]|7[ABCEGHJ-NPRSTV-Y]|8[ABCGJ-NPRSTV-Z]|9[ABCEGHJ-NPRSTV-Z])|X(0[ABCGX]|1[A])|Y(0[AB]|1[A]))[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$
    
  • US ZIP codes

    ^[0-9]{5}(-[0-9]{4})?$
    
  • UK post codes

    ^([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))$
    

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.

问题是,你可能没有很好的方法来跟上地球另一端国家不断变化的邮政编码要求,而你们没有共同的语言。除非你有足够大的预算来跟踪这一点,你几乎肯定更好的责任验证地址谷歌或雅虎。

两家公司都通过可编程API提供地址查找功能。

除了航运之外,有一个准确的邮政编码还有其他原因。从事跨境旅游的旅行社(当然欧元区除外)需要提前向当局提供这些信息。通常这些信息是由一个可能熟悉也可能不熟悉这些东西的代理输入的。任何能减少错误的方法都是好主意。

然而,编写一个覆盖世界上所有邮政编码的正则表达式将是疯狂的。

unicode CLDR包含每个国家的邮政编码正则表达式。(总共158个正则表达式!)

谷歌还有一个web服务,提供每个国家的地址格式信息,包括邮政编码,这里- http://i18napis.appspot.com/address (我发现链接通过http://unicode.org/review/pri180/)

编辑

下面是postalCodeData.xml regex的副本:

"GB", "GIR[ ]?0AA|((AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|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})?"
"BD", "\d{4}"
"BB", "(BB\d{5})?"
"BY", "\d{6}"
"BM", "[A-Z]{2}[ ]?[A-Z0-9]{2}"
"BA", "\d{5}"
"IO", "BBND 1ZZ"
"BN", "[A-Z]{2}[ ]?\d{4}"
"BG", "\d{4}"
"KH", "\d{5}"
"CV", "\d{4}"
"CL", "\d{7}"
"CR", "\d{4,5}|\d{3}-\d{4}"
"HR", "\d{5}"
"CY", "\d{4}"
"CZ", "\d{3}[ ]?\d{2}"
"DO", "\d{5}"
"EC", "([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?"
"EG", "\d{5}"
"EE", "\d{5}"
"FO", "\d{3}"
"GE", "\d{4}"
"GR", "\d{3}[ ]?\d{2}"
"GL", "39\d{2}"
"GT", "\d{5}"
"HT", "\d{4}"
"HN", "(?:\d{5})?"
"HU", "\d{4}"
"IS", "\d{3}"
"IN", "\d{6}"
"ID", "\d{5}"
"IL", "\d{5}"
"JO", "\d{5}"
"KZ", "\d{6}"
"KE", "\d{5}"
"KW", "\d{5}"
"LA", "\d{5}"
"LV", "\d{4}"
"LB", "(\d{4}([ ]?\d{4})?)?"
"LI", "(948[5-9])|(949[0-7])"
"LT", "\d{5}"
"LU", "\d{4}"
"MK", "\d{4}"
"MY", "\d{5}"
"MV", "\d{5}"
"MT", "[A-Z]{3}[ ]?\d{2,4}"
"MU", "(\d{3}[A-Z]{2}\d{3})?"
"MX", "\d{5}"
"MD", "\d{4}"
"MC", "980\d{2}"
"MA", "\d{5}"
"NP", "\d{5}"
"NZ", "\d{4}"
"NI", "((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?"
"NG", "(\d{6})?"
"OM", "(PC )?\d{3}"
"PK", "\d{5}"
"PY", "\d{4}"
"PH", "\d{4}"
"PL", "\d{2}-\d{3}"
"PR", "00[679]\d{2}([ \-]\d{4})?"
"RO", "\d{6}"
"RU", "\d{6}"
"SM", "4789\d"
"SA", "\d{5}"
"SN", "\d{5}"
"SK", "\d{3}[ ]?\d{2}"
"SI", "\d{4}"
"ZA", "\d{4}"
"LK", "\d{5}"
"TJ", "\d{6}"
"TH", "\d{5}"
"TN", "\d{4}"
"TR", "\d{5}"
"TM", "\d{6}"
"UA", "\d{5}"
"UY", "\d{5}"
"UZ", "\d{6}"
"VA", "00120"
"VE", "\d{4}"
"ZM", "\d{5}"
"AS", "96799"
"CC", "6799"
"CK", "\d{4}"
"RS", "\d{6}"
"ME", "8\d{4}"
"CS", "\d{5}"
"YU", "\d{5}"
"CX", "6798"
"ET", "\d{4}"
"FK", "FIQQ 1ZZ"
"NF", "2899"
"FM", "(9694[1-4])([ \-]\d{4})?"
"GF", "9[78]3\d{2}"
"GN", "\d{3}"
"GP", "9[78][01]\d{2}"
"GS", "SIQQ 1ZZ"
"GU", "969[123]\d([ \-]\d{4})?"
"GW", "\d{4}"
"HM", "\d{4}"
"IQ", "\d{5}"
"KG", "\d{6}"
"LR", "\d{4}"
"LS", "\d{3}"
"MG", "\d{3}"
"MH", "969[67]\d([ \-]\d{4})?"
"MN", "\d{6}"
"MP", "9695[012]([ \-]\d{4})?"
"MQ", "9[78]2\d{2}"
"NC", "988\d{2}"
"NE", "\d{4}"
"VI", "008(([0-4]\d)|(5[01]))([ \-]\d{4})?"
"PF", "987\d{2}"
"PG", "\d{3}"
"PM", "9[78]5\d{2}"
"PN", "PCRN 1ZZ"
"PW", "96940"
"RE", "9[78]4\d{2}"
"SH", "(ASCN|STHL) 1ZZ"
"SJ", "\d{4}"
"SO", "\d{5}"
"SZ", "[HLMS]\d{3}"
"TC", "TKCA 1ZZ"
"WF", "986\d{2}"
"XK", "\d{5}"
"YT", "976\d{2}"

使用这些regx

$ZIPREG=array(
"US"=>"^\d{5}([\-]?\d{4})?$",
"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
"DE"=>"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
"CA"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
"FR"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
"IT"=>"^(V-|I-)?[0-9]{5}$",
"AU"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
"NL"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
"ES"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
"DK"=>"^([D|d][K|k]( |-))?[1-9]{1}[0-9]{3}$",
"SE"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
"BE"=>"^[1-9]{1}[0-9]{3}$",
"IN"=>"^\d{6}$"
);

有人问关于格式化邮件地址的列表,我想这就是他要找的…

Frank的邮政地址强制指南:http://www.columbia.edu/~fdc/postal/ 然而,这对解决街头问题没有太大帮助 我的工作使用了一些工具来帮助实现这一点: - Lexis-Nexis服务,包括NCOA查询(您将“免费”获得地址标准化) - "Melissa Data" http://www.melissadata.com

.
.*

Big Jump忘记了换行符、空格和控制字符。

国际邮政编码是一个难以解决的问题。

这是一个非常简单的RegEx,用于验证美国邮政编码(而不是邮政编码+ 4):

(?!([089])\1{4})\d{5}

似乎所有的五位数字都是有效的邮政编码,除了0000088888 &99999

我已经用http://regexpal.com/测试了这个RegEx

SP

  1. 每个邮政编码系统只使用A-Z和/或0-9,有时还使用空格/破折号

  2. 并不是每个国家都使用邮政编码(例如都柏林以外的爱尔兰),但我们在这里忽略它。

  3. 最短的邮政编码格式是塞拉利昂的NN

  4. 最长的是美属萨摩亚,NNNNN-NNNNNN

  5. 你应该留出一个空格或破折号。

  6. 不应该以空格或破折号开始或结束

这应包括上述内容:

(?i)^[a-z0-9][a-z0-9\- ]{0,10}[a-z0-9]$
请注意,这是一个相当难的问题,正如公认的答案所述。 我想这并没有阻止geonames.org的人。 他们有一个文件国家信息文件,这并不完全符合这个答案-限制是30000字符显然。大约有150个国家的正则表达式

我在这里提取了与这个问题相关的部分:

AD ^(?:AD)*(\d{3})$
AM ^(\d{6})$
AR ^([A-Z]\d{4}[A-Z]{3})$
AT ^(\d{4})$
AU ^(\d{4})$
AX ^(?:FI)*(\d{5})$
AZ ^(?:AZ)*(\d{4})$
BA ^(\d{5})$
BB ^(?:BB)*(\d{5})$
BD ^(\d{4})$
BE ^(\d{4})$
BG ^(\d{4})$
BH ^(\d{3}\d?)$
BM ^([A-Z]{2}\d{2})$
BN ^([A-Z]{2}\d{4})$
BR ^(\d{8})$
BY ^(\d{6})$
CA ^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]) ?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$
CH ^(\d{4})$
CL ^(\d{7})$
CN ^(\d{6})$
CR ^(\d{4})$
CU ^(?:CP)*(\d{5})$
CV ^(\d{4})$
CX ^(\d{4})$
CY ^(\d{4})$
CZ ^(\d{5})$
DE ^(\d{5})$
DK ^(\d{4})$
DO ^(\d{5})$
DZ ^(\d{5})$
EC ^([a-zA-Z]\d{4}[a-zA-Z])$
EE ^(\d{5})$
EG ^(\d{5})$
ES ^(\d{5})$
ET ^(\d{4})$
FI ^(?:FI)*(\d{5})$
FM ^(\d{5})$
FO ^(?:FO)*(\d{3})$
FR ^(\d{5})$
GB ^(([A-Z]\d{2}[A-Z]{2})|([A-Z]\d{3}[A-Z]{2})|([A-Z]{2}\d{2}[A-Z]{2})|([A-Z]{2}\d{3}[A-Z]{2})|([A-Z]\d[A-Z]\d[A-Z]{2})|([A-Z]{2}\d[A-Z]\d[A-Z]{2})|(GIR0AA))$
GE ^(\d{4})$
GF ^((97|98)3\d{2})$
GG ^(([A-Z]\d{2}[A-Z]{2})|([A-Z]\d{3}[A-Z]{2})|([A-Z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
MT ^([A-Z]{3}\d{2}\d?)$
MV ^(\d{5})$
MX ^(\d{5})$
MY ^(\d{5})$
MZ ^(\d{4})$
NC ^(\d{5})$
NE ^(\d{4})$
NF ^(\d{4})$
NG ^(\d{6})$
NI ^(\d{7})$
NL ^(\d{4}[A-Z]{2})$
NO ^(\d{4})$
NP ^(\d{5})$
NZ ^(\d{4})$
OM ^(\d{3})$
PF ^((97|98)7\d{2})$
PG ^(\d{3})$
PH ^(\d{4})$
PK ^(\d{5})$
PL ^(\d{5})$
PM ^(97500)$
PR ^(\d{9})$
PT ^(\d{7})$
PW ^(96940)$
PY ^(\d{4})$
RE ^((97|98)(4|7|8)\d{2})$
RO ^(\d{6})$
RS ^(\d{6})$
RU ^(\d{6})$
SA ^(\d{5})$
SD ^(\d{5})$
SE ^(?:SE)*(\d{5})$
SG ^(\d{6})$
SH ^(STHL1ZZ)$
SI ^(?:SI)*(\d{4})$
SK ^(\d{5})$
SM ^(4789\d)$
SN ^(\d{5})$
SO ^([A-Z]{2}\d{5})$
SV ^(?:CP)*(\d{4})$
SZ ^([A-Z]\d{3})$
TC ^(TKCA 1ZZ)$
TH ^(\d{5})$
TJ ^(\d{6})$
TM ^(\d{6})$
TN ^(\d{4})$
TR ^(\d{5})$
TW ^(\d{5})$
UA ^(\d{5})$
US ^\d{5}(-\d{4})?$
UY ^(\d{5})$
UZ ^(\d{6})$
VA ^(\d{5})$
VE ^(\d{4})$
VI ^\d{5}(-\d{4})?$
VN ^(\d{6})$
WF ^(986\d{2})$
YT ^(\d{5})$
ZA ^(\d{4})$
ZM ^(\d{5})$
CS ^(\d{5})$

希望我没有犯错,我的regex-fu很弱。

如果有人仍然对如何验证邮政编码感兴趣,我找到了一个解决方案:

使用Google Geocoding API,我们可以检查具有国家代码邮政编码代码本身的邮政编码的有效性。

例如,我住在乌克兰,所以我可以这样检查: https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:80380|country:UA < / p >

或者使用JS API: https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering

其中80380是乌克兰有效的ZIP,实际上every(#####)是有效的。

谷歌如果没有找到则返回ZERO_RESULTS状态。 或OK和一个结果,如果两者都是正确的

希望这对你有所帮助。

正如其他人指出的那样,用一个正则表达式来统治所有正则表达式是不可能的。然而,你可以使用来自万国邮政联盟的地址格式信息(一个鲜为人知的联合国机构)为你需要的尽可能多的国家创建正则表达式。

例如,以下是一些国家的地址格式规则,包括邮政编码(PDF格式):

如果Zip Code允许字符和数字(字母数字),则在匹配的地方使用below regex, 5或9或10个字母数字字符加上一个连字符(-):

^([0-9A-Za-z]{5}|[0-9A-Za-z]{9}|(([0-9a-zA-Z]{5}-){1}[0-9a-zA-Z]{4}))$
我知道这是一个老问题,但我无意中遇到了同样的问题。 我有来自100多个国家的发票,并试图得到正确的债权人在zip(如果每其他检查失败)。 所以我所做的就是写一个简短的Python脚本,从字符串创建一个模式:

class RegexPatternBuilder:
"""
Builds a regex pattern out of a given string(i.e. --> HM452 AX2155 : [A-Z]{2}\d{3}\s{1}[A-Z]{2}\d{4})
"""
__is_alpha_count = 0
__is_numeric_count = 0
__is_whitespace_count = 0
__pattern = ""


# Count: wich character of the string we're locking at right now
__count = 0


# Countrys like  Andora starts theire ZIP with the country abbreviation :AD500
# So check at first if the ZIP starts with the abbreviation and if so, add it to the pattern and increase the count.
def __init__(self, zip_string, country):
self.__zip_string = zip_string
self.__country = country
if self.__zip_string.startswith(country):
self.__pattern = f'({self.__country})'
self.__count += len(self.__country)


def build_regex(self):
# Last step ;
# Add the current alpha_numeric pattern with count
if len(self.__zip_string) == self.__count:
if self.__is_alpha_count:
self.__pattern += f"[A-Z]\{\{{self.__is_alpha_count}}}"
if self.__is_numeric_count:
self.__pattern += f"\d\{\{{self.__is_numeric_count}}}"
return f'{self.__pattern}\\b'


# Case: Whitespace
# Check if there is a crossing from numeric / alphanumeric to whitespace,
# if so --> add the alpha_numeric regex to the whole pattern with the
# count as the number of viable appeaerances.
# Since there is max 1 whitespace in a ZIP, add the whitespace regex immediately.
# Every other case is similar to that.
if self.__zip_string[self.__count].isspace():
if self.__is_numeric_count:
self.__pattern += f"\d\{\{{self.__is_numeric_count}}}"
if self.__is_alpha_count:
self.__pattern += f"[A-Z]\{\{{self.__is_alpha_count}}}"
self.__pattern += "\s{1}"
self.__is_whitespace_count += 1
self.__is_alpha_count = 0
self.__is_numeric_count = 0


# Case: Is Alphanumeric
if self.__zip_string[self.__count].isalpha():
if self.__is_numeric_count:
self.__pattern += f"[0-9]\{\{{self.__is_numeric_count}}}"
self.__is_whitespace_count = 0
self.__is_alpha_count += 1
self.__is_numeric_count = 0


# Case: Is Numeric
if self.__zip_string[self.__count].isnumeric():
if self.__is_alpha_count:
self.__pattern += f"[A-Z]\{\{{self.__is_alpha_count}}}"
self.__is_whitespace_count = 0
self.__is_alpha_count = 0
self.__is_numeric_count += 1


# Case: Special Character (i.e. - )
# No escaping or count for this so far, because it shouldn't be needed for our zip purposes
if not self.__zip_string[self.__count].isalpha() \
and not self.__zip_string[self.__count].isnumeric() \
and not self.__zip_string[self.__count].isspace():
self.__pattern += f'{self.__zip_string[self.__count]}\{\{1}}'
self.__count += 1
return self.build_regex()
有了它,我为所有zip(按国家)创建了所有不同的可能的正则表达式,我们历史上把它们写回一个db表(即这样的东西在最后: COUNTRY:RE PATTERN:(\d{5})\b[这可能是任何国家;D])

也许它能帮助别人。