我有一个字符串:
5956 Executive Dr #101, Fitchburg, WI 53719
我希望它是:
5956 Executive Dr, Fitchburg, WI 53719
我试过:
string2 = re.sub("(\.*)\s+#.*", "\1", string1)
但这只会让我:
5956 Executive Dr
我如何在Python中做到这一点?我觉得重新表述太令人困惑了。理想情况下,我想对Apt、Unit等做同样的事情——替换任何像";公寓3"第三单元"套房4";用"&";。
我认为你的方法不是最好的,你可以使用一个更简单的表达式:
string2 = re.sub("s#d+", "", string1)
- \s-空白
- #-文字Octhorpe
- \d+-至少一位
要移除其他部件,可以使用"(Apt|Unit|Suite)sd+"
或"s#d+|(Apt|Unit|Suite)sd+"