我正在寻找最终的邮政编码和邮政编码正则表达式。我正在寻找一种能覆盖世界大部分地区(希望是所有地区)的东西。
当前回答
我知道这是一个老问题,但我无意中遇到了同样的问题。 我有来自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()
有了这个,我创建了所有不同的可能的正则表达式的所有拉链(按国家),我们历史上写回一个db表(即这样的东西在最后: 国家:RE PATTERN:(\d{5})\b[这可能是什么国家;d])
也许它能帮助别人。
其他回答
你为什么要这么做,你为什么这么在乎?正如Tom Ritter所指出的,你是否有一个ZIP/邮政编码并不重要,更不用说它是否有效了,除非你真的要把东西发送到那个地址。即使你希望有一天你会给他们寄东西,这并不意味着你今天就需要邮政编码。
每个邮政编码系统只使用A-Z和/或0-9,有时还使用空格/破折号 并不是每个国家都使用邮政编码(例如都柏林以外的爱尔兰),但我们在这里忽略它。 最短的邮政编码格式是塞拉利昂NN 最长的是美属萨摩亚,有NNNNN-NNNNNN 你应该留出一个空格或破折号。 不应该以空格或破折号开始或结束
这应包括上述内容:
(?i)^[a-z0-9][a-z0-9\- ]{0,10}[a-z0-9]$
我们使用以下方法:
加拿大
([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
其他
接受现状
没有。
世界各地的邮政/邮政编码并不遵循一个共同的模式。在一些国家,它们由数字组成,在其他国家,它们可以是数字和字母的组合,有些可以包含空格,有些可以包含点,字符的数量从2个到至少6个不等……
你能做的(理论上)是为世界上每个国家创建一个单独的正则表达式,我不推荐。但是您仍然会忽略验证部分:邮政编码12345可能存在,但12346不存在,也许12344也不存在。你如何用正则表达式检查它?
你不能。
如果有人仍然对如何验证邮政编码感兴趣,我找到了一个解决方案:
使用谷歌地理编码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是乌克兰有效的ZIP,实际上每个(#####)是有效的。
如果没有发现,谷歌返回ZERO_RESULTS状态。 或者OK和一个结果,如果两者都正确。
希望这对你有所帮助。
推荐文章
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 正则表达式中的单词边界是什么?
- 如何将一个标题转换为jQuery的URL段塞?
- Javascript和regex:分割字符串并保留分隔符
- (grep)正则表达式匹配非ascii字符?
- 如何在保持原始字符串的同时对字符串执行Perl替换?
- 创建正则表达式匹配数组
- *的区别是什么?和。*正则表达式?
- 如何将“camelCase”转换为“Camel Case”?
- 在Java中使用正则表达式提取值
- Java中的正则表达式命名组
- 使用正则表达式搜索和替换Visual Studio代码
- 使用split("|")按管道符号拆分Java字符串
- 替换字符串中第一次出现的模式
- “\d”在正则表达式中是数字吗?