我正在寻找最终的邮政编码和邮政编码正则表达式。我正在寻找一种能覆盖世界大部分地区(希望是所有地区)的东西。
当前回答
每个邮政编码系统只使用A-Z和/或0-9,有时还使用空格/破折号 并不是每个国家都使用邮政编码(例如都柏林以外的爱尔兰),但我们在这里忽略它。 最短的邮政编码格式是塞拉利昂NN 最长的是美属萨摩亚,有NNNNN-NNNNNN 你应该留出一个空格或破折号。 不应该以空格或破折号开始或结束
这应包括上述内容:
(?i)^[a-z0-9][a-z0-9\- ]{0,10}[a-z0-9]$
其他回答
考虑到每个国家都有如此多的边缘情况(例如。伦敦地址可能使用与英国其他地区略有不同的格式)我不认为有一个终极正则表达式,除了可能:
[0-9a-zA-Z]+
最好是使用一个相当广泛的模式(好吧,不像上面那么广泛),或者用自己的特定模式对待每个国家/地区!
更新:然而,它可能动态地构造一个正则表达式基于许多更小的,特定于区域的规则-虽然不确定性能!
在RegExLib网站上可以找到许多特定国家的模式。
问题是,你可能没有很好的方法来跟上地球另一端国家不断变化的邮政编码要求,而你们没有共同的语言。除非你有足够大的预算来跟踪这一点,你几乎肯定更好的责任验证地址谷歌或雅虎。
两家公司都通过可编程API提供地址查找功能。
我知道这是一个老问题,但我无意中遇到了同样的问题。 我有来自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])
也许它能帮助别人。
正如其他地方指出的那样,世界各地的差异很大。即使某些东西符合模式也不意味着它存在。
当然,还有很多地方不使用邮政编码(例如much或爱尔兰)。
除了航运之外,有一个准确的邮政编码还有其他原因。从事跨境旅游的旅行社(当然欧元区除外)需要提前向当局提供这些信息。通常这些信息是由一个可能熟悉也可能不熟悉这些东西的代理输入的。任何可以减少错误的方法都是好主意
然而,编写一个覆盖世界上所有邮政编码的正则表达式将是疯狂的。
推荐文章
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 正则表达式中的单词边界是什么?
- 如何将一个标题转换为jQuery的URL段塞?
- Javascript和regex:分割字符串并保留分隔符
- (grep)正则表达式匹配非ascii字符?
- 如何在保持原始字符串的同时对字符串执行Perl替换?
- 创建正则表达式匹配数组
- *的区别是什么?和。*正则表达式?
- 如何将“camelCase”转换为“Camel Case”?
- 在Java中使用正则表达式提取值
- Java中的正则表达式命名组
- 使用正则表达式搜索和替换Visual Studio代码
- 使用split("|")按管道符号拆分Java字符串
- 替换字符串中第一次出现的模式
- “\d”在正则表达式中是数字吗?