如何在字符串中间提取zipcode?

时间:2021-02-14 19:16:46

Here is an address:

这是一个地址:

address = "35 rue de trucmuche, 75009 PARIS"

I want to extract the zipcode (75009) in the address using a Regex.

我想使用正则表达式在地址中提取zipcode(75009)。

I tried this:

我试过这个:

reg = re.compile('^.*(P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009

I get a:

我得到一个:

AttributeError: 'NoneType' object has no attribute 'groupdict'

AttributeError:'NoneType'对象没有属性'groupdict'

I think my Regex is wrong. I can't understand why.

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

3 个解决方案

#1


You are just missing the ? in the named capturing group:

你只是错过了?在指定的捕获组中:

^.*(?P<zipcode>\d{5}).*$

reg = re.compile('^.*(?P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009

#2


A named capture group in Python must start with ?:

Python中的命名捕获组必须以?开头:

>>> import re
>>> address = "35 rue de trucmuche, 75009 PARIS"
>>> re.match('^.*(?P<zipcode>\d{5}).*$', address).groupdict()['zipcode']
'75009'

Otherwise, you will be trying to match the literal text P<zipcode>.

否则,您将尝试匹配文字文本P


Also, the .groupdict() method returns a normal Python dictionary:

此外,.groupdict()方法返回一个普通的Python字典:

>>> type(re.match('^.*(?P<zipcode>\d{5}).*$', address).groupdict())
<class 'dict'>

This means that you will need to access the zipcode value as dct['zipcode'], not dct.zipcode.

这意味着您需要访问zipcode值为dct ['zipcode'],而不是dct.zipcode。

#3


Your regex is wrong. That is why it won't match, it returns None, and complains that None doesn't have a groupdict().

你的正则表达式是错的。这就是为什么它不匹配,它返回None,并抱怨None没有groupdict()。

In fact there are two mistakes as far as I can see.

事实上,就我所见,有两个错误。

reg = re.compile('^.*(?P<zipcode>\d{5}).*$')

------------------------------------^--------------------- (needs a '?' in front of 'P')

------------------------------------ ^ ------------- --------(需要在'P'前面'?')

and the other mistake that will come up is that groupdict() nees to be accessed like a normal dict, that is

而另一个错误就是groupdict()需要像普通的dict一样被访问,也就是说

match.groupdict()['zipcode']

You should probably also put a check to see that the match matches, e.g.

您可能还应该检查匹配是否匹配,例如

if match:
     match.groupdict()['zipcode']

as according to https://docs.python.org/2/library/re.html#match-objects the match object will return True if it exists.

根据https://docs.python.org/2/library/re.html#match-objects,如果匹配对象存在,则返回True。

#1


You are just missing the ? in the named capturing group:

你只是错过了?在指定的捕获组中:

^.*(?P<zipcode>\d{5}).*$

reg = re.compile('^.*(?P<zipcode>\d{5}).*$')
match = reg.match(address)
match.groupdict().zipcode # should be 75009

#2


A named capture group in Python must start with ?:

Python中的命名捕获组必须以?开头:

>>> import re
>>> address = "35 rue de trucmuche, 75009 PARIS"
>>> re.match('^.*(?P<zipcode>\d{5}).*$', address).groupdict()['zipcode']
'75009'

Otherwise, you will be trying to match the literal text P<zipcode>.

否则,您将尝试匹配文字文本P


Also, the .groupdict() method returns a normal Python dictionary:

此外,.groupdict()方法返回一个普通的Python字典:

>>> type(re.match('^.*(?P<zipcode>\d{5}).*$', address).groupdict())
<class 'dict'>

This means that you will need to access the zipcode value as dct['zipcode'], not dct.zipcode.

这意味着您需要访问zipcode值为dct ['zipcode'],而不是dct.zipcode。

#3


Your regex is wrong. That is why it won't match, it returns None, and complains that None doesn't have a groupdict().

你的正则表达式是错的。这就是为什么它不匹配,它返回None,并抱怨None没有groupdict()。

In fact there are two mistakes as far as I can see.

事实上,就我所见,有两个错误。

reg = re.compile('^.*(?P<zipcode>\d{5}).*$')

------------------------------------^--------------------- (needs a '?' in front of 'P')

------------------------------------ ^ ------------- --------(需要在'P'前面'?')

and the other mistake that will come up is that groupdict() nees to be accessed like a normal dict, that is

而另一个错误就是groupdict()需要像普通的dict一样被访问,也就是说

match.groupdict()['zipcode']

You should probably also put a check to see that the match matches, e.g.

您可能还应该检查匹配是否匹配,例如

if match:
     match.groupdict()['zipcode']

as according to https://docs.python.org/2/library/re.html#match-objects the match object will return True if it exists.

根据https://docs.python.org/2/library/re.html#match-objects,如果匹配对象存在,则返回True。