Python地址格式验证

202 阅读3分钟

在 Python 中进行地址格式验证,通常会根据具体的应用场景验证 URL 地址、IP 地址、邮寄地址或邮箱地址。

1、问题背景

用户需要在程序中输入一个地址,但这个地址必须满足特定的格式,否则视为输入错误。地址示例如下:717 N 2ND ST, MANKATO, MN 56001717 N 2ND ST, MANKATO, MN, 56001

2、解决方案

为了确保地址格式正确,需要使用 Python 的正则表达式进行验证。下面提供了两种正则表达式,满足上述要求:

正则表达式1:

regex = r'''
    (?x)            # verbose regular expression
    (?i)            # ignore case
    (?P<HouseNumber>\d+)\s+         # Matches '717 '
    (?P<Direction>[news])\s+        # Matches 'N '
    (?P<StreetName>\w+)\s*          # Matches '2ND ', with optional trailing space
    (?P<StreetDesignator>\w*)\s*    # Optionally Matches 'ST '
    (?P<StreetDirection>[news]*)\s* # Optionally Matches 'NE'
    ,\s+                            # Force a comma after the street
    (?P<TownName>.*),\s+            # Matches 'MANKATO, '
    (?P<State>[A-Z]{2}),?\s+        # Matches 'MN ' and 'MN, '
    (?P<ZIP>\d{5})                  # Matches '56001'
'''

正则表达式2:

\d{1,6}\s\w+\s\w+\s[A-Za-z]{2},\s([A-Za-z]+),\s[A-Za-z]{2}(,\s\d{1,6}|\s\d{1,6})

代码示例:

import re
​
# 正则表达式
regex = r'''
    (?x)            # verbose regular expression
    (?i)            # ignore case
    (?P<HouseNumber>\d+)\s+         # Matches '717 '
    (?P<Direction>[news])\s+        # Matches 'N '
    (?P<StreetName>\w+)\s*          # Matches '2ND ', with optional trailing space
    (?P<StreetDesignator>\w*)\s*    # Optionally Matches 'ST '
    (?P<StreetDirection>[news]*)\s* # Optionally Matches 'NE'
    ,\s+                            # Force a comma after the street
    (?P<TownName>.*),\s+            # Matches 'MANKATO, '
    (?P<State>[A-Z]{2}),?\s+        # Matches 'MN ' and 'MN, '
    (?P<ZIP>\d{5})                  # Matches '56001'
'''
​
# 编译正则表达式
regex = re.compile(regex)
​
# 地址列表
addresses = [    '717 N 2ND ST, MANKATO, MN 56001',    '717 N 2ND ST, MANKATO, MN, 56001',    '717 N 2ND, Makata, 56001',   # 应拒绝此地址    '1234 N D AVE, East Boston, MA, 02134',    '717 N 2ND ST NE, MANKATO, MN, 56001',    '717 N 2ND, MANKATO, MN, 56001',]
​
# 逐一匹配地址
for address in addresses:
    match = regex.match(address)
    print(f'地址: {address}')
    if match:
        print("    地址格式正确")
        print("    门牌号: {}".format(match.group('HouseNumber')))
        print("    方向: {}".format(match.group('Direction')))
        print("    街道名称: {}".format(match.group('StreetName')))
        print("    街道指示符: {}".format(match.group('StreetDesignator')))
        print("    街道方向: {}".format(match.group('StreetDirection')))
        print("    城市名称: {}".format(match.group('TownName')))
        print("    州: {}".format(match.group('State')))
        print("    邮编: {}".format(match.group('ZIP')))
    else:
        print("    地址格式错误")

运行结果:

地址: 717 N 2ND ST, MANKATO, MN 56001
    地址格式正确
    门牌号: 717
    方向: N
    街道名称: 2ND
    街道指示符: ST
    街道方向: None
    城市名称: MANKATO
    州: MN
    邮编: 56001
地址: 717 N 2ND ST, MANKATO, MN, 56001
    地址格式正确
    门牌号: 717
    方向: N
    街道名称: 2ND
    街道指示符: ST
    街道方向: None
    城市名称: MANKATO
    州: MN
    邮编: 56001
地址: 717 N 2ND, Makata, 56001
    地址格式错误
地址: 1234 N D AVE, East Boston, MA, 02134
    地址格式正确
    门牌号: 1234
    方向: N
    街道名称: D
    街道指示符: AVE
    街道方向: None
    城市名称: East Boston
    州: MA
    邮编: 02134
地址: 717 N 2ND ST NE, MANKATO, MN, 56001
    地址格式正确
    门牌号: 717
    方向: N
    街道名称: 2ND
    街道指示符: ST
    街道方向: NE
    城市名称: MANKATO
    州: MN
    邮编: 56001
地址: 717 N 2ND, MANKATO, MN, 56001
    地址格式错误

总结

  • 使用内置模块如 validatorsipaddress 可以轻松验证 URL、IP 和邮箱。
  • 正则表达式提供了灵活性,可根据需求定制验证规则。
  • 地理地址和邮政编码的验证通常依赖 API 或自定义规则。
  • 编写通用验证函数可以提高复用性和扩展性。