我想過濾街道名稱,並得到我想要的部分。這些名字有幾種格式。這裏有一些例子,我想從他們那裏得到什麼。正則表達式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