Python re 表达式 - 删除 # 后跟数字直到逗号

Python re expression - remove # followed by numbers up until a comma

我有一个字符串:

5956 Executive Dr #101, Fitchburg, WI 53719

我希望它是:

5956 Executive Dr, Fitchburg, WI 53719

我试过:

string2 = re.sub("(\.*)\s+#.*", "\1", string1)

但这让我很感动:

5956 Executive Dr

如何在 Python 中执行此操作?我发现 re 表达式很混乱。理想情况下,我想对 Apt、Unit 等做同样的事情——用“”替换任何实例,如“Apt 3”、“Unit 3”、“Suite 4”。

我认为你的方法在这里不是最好的,你可以使用更简单的表达方式:

string2 = re.sub("\s#\d+", "", string1)
  • \s - 空格
  • # - 文字八爪鱼
  • \d+ - 至少一位数字

要同时删除其他部分,您可以使用 "(Apt|Unit|Suite)\s\d+" 或一起使用 "\s#\d+|(Apt|Unit|Suite)\s\d+"