假设我有一个由不同句子组成的字符串。我期望删除以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.
如何调整正则表达式以达到这个结果?为什么它会这样?
1条答案
按热度按时间sz81bmfz1#
使用
+?
使匹配不贪婪。