这是一个地址:
address = "35 rue de trucmuche, 75009 PARIS"
Run Code Online (Sandbox Code Playgroud)
我想75009使用正则表达式提取地址中的邮政编码 ( )。
我试过这个:
reg = re.compile('^.*(P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009
Run Code Online (Sandbox Code Playgroud)
我得到一个:
AttributeError: 'NoneType' object has no attribute 'groupdict'
我认为我的正则表达式是错误的。我不明白为什么。
你只是想念?在命名的捕获组中:
^.*(?P<zipcode>\d{5}).*$
Run Code Online (Sandbox Code Playgroud)
reg = re.compile('^.*(?P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3776 次 |
| 最近记录: |