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

时间:2015-05-06 21:39:01

标签: python regex

这是一个地址:

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' object has no attribute 'groupdict'

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

3 个答案:

答案 0 :(得分:4)

Python中的命名捕获组必须以var locations = [ ['Italy 3', 45.343294, 8.853324, 16], ['Italy 2', 44.534529, 10.303519, 15], ['Italy 1', 41.416810, 15.313285, 14], ['Bulgaria', 42.235462, 23.838675, 13], ['Albania', 41.317868, 20.147269, 12], ['Turkey 2', 39.714402, 32.803519, 11], ['Turkey 1', 37.968918, 29.200003, 10], ['Romania 2', 44.251917, 25.464652, 9], ['Romania 1', 45.651325, 22.476371, 8], ['Bosnia', 43.872979, 17.246879, 7], ['Serbia', 43.841292, 20.718558, 6], ['Athens', 37.977984, 23.741343, 4], ['Istanbul', 41.2440257, 29.0616179, 5], ['Rome', 41.851999, 12.555716, 3], ['Berlin', 52.481801, 13.291800, 2], ['Beirut', 33.787747, 35.796924, 1] ]; var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { var markernumber = Math.floor((Math.random() * 100) + 1); marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map, icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + markernumber + '|F6DD0E|000000' }); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i][0] + '<br>There are ' + markernumber + ' stories.'); infowindow.open(map, marker); } })(marker, i)); } 开头:

?

否则,您将尝试匹配文字文本>>> import re >>> address = "35 rue de trucmuche, 75009 PARIS" >>> re.match('^.*(?P<zipcode>\d{5}).*$', address).groupdict()['zipcode'] '75009'

此外,P<zipcode>方法返回普通的Python字典:

.groupdict()

这意味着您需要以>>> type(re.match('^.*(?P<zipcode>\d{5}).*$', address).groupdict()) <class 'dict'> 而不是dct['zipcode']来访问邮政编码值。

答案 1 :(得分:4)

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

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

答案 2 :(得分:0)

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

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

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

------------------------------------ ^ ---------- ----------- (需要在&#39; P&#39;之前)&#39;

;

而另一个错误就是groupdict()可以像普通dict那样被访问,即

match.groupdict()['zipcode']

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

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

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