Python中的正则表达式没有返回所需的结果

flseospp  于 2023-03-21  发布在  Python
关注(0)|答案(1)|浏览(100)

假设我有一个由不同句子组成的字符串。我期望删除以It was formerly known as开头的部分,直到这句话的结尾。我想停止清理,直到它达到. Withey Limited。如果不是这样,它结束清理,直到. It

import re
txt = 'It was formerly known as A. Withey & Black Limited. Withey Limited delivers many things. It has a facility in the UK, including many branches.'
out = re.sub("\s*It was formerly known as [\w\d\s@_!#$%^&*()<>?/\|}{~:\.]+" + "(?=(. Withey Limited |. It))","", txt)

这段代码返回. It has a facility in the UK, including many branches.',这不是我的预期结果。我的预期结果如下:

Withey Limited delivers many things. It has a facility in the UK, including many branches.

如何调整正则表达式以达到这个结果?为什么它会这样?

sz81bmfz

sz81bmfz1#

使用+?使匹配不贪婪。

out = re.sub(r"\s*It was formerly known as [\w\d\s@_!#$%^&*()<>?/\|}{~:\.]+?\. " + "(?=(Withey Limited|It))","", txt)

相关问题