我正在寻找最终的邮政编码和邮政编码正则表达式。我正在寻找一种能覆盖世界大部分地区(希望是所有地区)的东西。
当前回答
根据您的应用程序,您可能希望对大多数访问者来自的国家实现正则表达式匹配,而对其余的国家不进行验证(接受任何东西)。
其他回答
有人问关于格式化邮件地址的列表,我想这就是他要找的…
弗兰克的邮政地址强制指南:http://www.columbia.edu/~fdc/postal/ 但这对解决街头问题没什么帮助。
我的工作使用了一些工具来帮助实现这一点: - Lexis-Nexis服务,包括NCOA查询(您将“免费”获得地址标准化) -“梅丽莎数据”http://www.melissadata.com
除了航运之外,有一个准确的邮政编码还有其他原因。从事跨境旅游的旅行社(当然欧元区除外)需要提前向当局提供这些信息。通常这些信息是由一个可能熟悉也可能不熟悉这些东西的代理输入的。任何可以减少错误的方法都是好主意
然而,编写一个覆盖世界上所有邮政编码的正则表达式将是疯狂的。
正如其他人指出的那样,用一个正则表达式来统治所有正则表达式是不可能的。但是,您可以使用来自万国邮政联盟(一个鲜为人知的联合国机构)的地址格式信息为尽可能多的国家创建正则表达式。
例如,以下是一些国家的地址格式规则,包括邮政编码(PDF格式):
加拿大 日本 瑞士 俄罗斯联邦 美利坚合众国
我知道这是一个老问题,但我无意中遇到了同样的问题。 我有来自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])
也许它能帮助别人。
没有。
世界各地的邮政/邮政编码并不遵循一个共同的模式。在一些国家,它们由数字组成,在其他国家,它们可以是数字和字母的组合,有些可以包含空格,有些可以包含点,字符的数量从2个到至少6个不等……
你能做的(理论上)是为世界上每个国家创建一个单独的正则表达式,我不推荐。但是您仍然会忽略验证部分:邮政编码12345可能存在,但12346不存在,也许12344也不存在。你如何用正则表达式检查它?
你不能。
推荐文章
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 正则表达式中的单词边界是什么?
- 如何将一个标题转换为jQuery的URL段塞?
- Javascript和regex:分割字符串并保留分隔符
- (grep)正则表达式匹配非ascii字符?
- 如何在保持原始字符串的同时对字符串执行Perl替换?
- 创建正则表达式匹配数组
- *的区别是什么?和。*正则表达式?
- 如何将“camelCase”转换为“Camel Case”?
- 在Java中使用正则表达式提取值
- Java中的正则表达式命名组
- 使用正则表达式搜索和替换Visual Studio代码
- 使用split("|")按管道符号拆分Java字符串
- 替换字符串中第一次出现的模式
- “\d”在正则表达式中是数字吗?