我想过滤街道名称,并得到我想要的部分。这些名字有几种格式。这里有一些例子,我想从他们那里得到什么。正则表达式python不会工作,因为我想它
Car Cycle 5 B Ap 1233 < what I have
Car Cycle 5 B < what I want
Potato street 13 1 AB < what I have
Potato street 13 < what I want
Chrome Safari 41 Ap 765 < what I have
Chrome Safari 41 < what I want
Highstreet 53 Ap 2632/BH < what I have
Highstreet 53 < what I want
Something street 91/Daniel < what I have
Something street 91 < what I want
通常我想要的是后面的街道号码街道名称(1-4名),如果有一个,然后在街上信(1号),如果有一个。我无法让它正常工作。
这里是我的代码(我知道,它吮吸):
import re
def address_regex(address):
regex1 = re.compile("(\w+){1,4}(\d{1,4}){1}(\w{1})")
regex2 = re.compile("(\w+){1,4}(\d{1,4}){1}")
regex3 = re.compile("(\w+){1,4}(\d){1,4}")
regex4 = re.compile("(\w+){1,4}(\w+)")
s1 = regex1.search(text)
s2 = regex2.search(text)
s3 = regex3.search(text)
s4 = regex4.search(text)
regex_address = ""
if s1 != None:
regex_address = s1.group()
elif s2 != None:
regex_address = s2.group()
elif s3 != None:
regex_address = s3.group()
elif s4 != None:
regex_address = s4.group()
else:
regex_address = address
return regex_address
我使用Python 3.4
只需使用像科多兽正则表达式的工具。 – bgusach
你不想要最后一个例子中的数字91? – Falko
最后一个例子的逻辑是什么? “街道91/Daniel'为什么不带91? – PYPL