如何提取字符串中间的邮政编码?

Dav*_* D. 2 python regex

这是一个地址:

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'

我认为我的正则表达式是错误的。我不明白为什么。

And*_*302 5

你只是想念?在命名的捕获组中:

^.*(?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)