这是一个地址:
address = "35 rue de trucmuche, 75009 PARIS"
我想使用正则表达式提取地址中的邮政编码 (75009)。
我试过这个:
reg = re.compile('^.*(P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009
我得到一个:
AttributeError: 'NoneType' 对象没有属性 'groupdict'
我认为我的正则表达式是错误的。我不明白为什么。
请您参考如下方法:
您只是错过了 ?在命名的捕获组中:
^.*(?P<zipcode>\d{5}).*$
reg = re.compile('^.*(?P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009
