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


当前回答

使用这些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}$"
);

其他回答

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

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

使用谷歌地理编码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和一个结果,如果两者都正确。

希望这对你有所帮助。

我知道这是一个老问题,但我无意中遇到了同样的问题。 我有来自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])

也许它能帮助别人。

请注意,这是一个相当难的问题,正如公认的答案所述。 我想这并没有阻止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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{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很弱。

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

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

似乎除了00000、88888和99999之外,所有五位数字都是有效的邮政编码。

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

SP