如何在re.sub()中为正则表达式搜索设置停止条件

2024-05-13 22:09:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本块,我想从中删除短语

"Adaptation" means a work based upon the Work, or cAt upon the Work and other pre-existing works, such as a f translation, DOG adaptation, derivative work, arrangement of music or other alterations of a literary or artistic work, or phonogram f or performance and includes cinematographic adaptations or any

我想删除cAtf之间DOGf之间或rugratsf之间的任何文本。为了方便起见,我突出显示了文本框中的术语

每个短语(cAtDOGrugratsf)前后各有一个空格

这是我的密码

clean = `TEXT SHOWN ABOVE`
segment_start = [' cAt ', ' DOG ', ' rugrats ']
segment_end = ' f ' 

for start in enumerate(segment_start):
    clean = re.sub('{}.*{}{1}'.format(start, segment_end), ' ', clean)

我想退出

"Adaptation" means a work based upon the Work, or cAt upon the Work and other pre-existing works, such as a f translation, DOG adaptation, derivative work, arrangement of music or other alterations of a literary or artistic work, or phonogram f or performance and includes cinematographic adaptations or any

然后就这样结束了

"Adaptation" means a work based upon the Work, or translation, or performance and includes cinematographic adaptations or any

我的代码出错了。它找到段的开始,然后找到段的最后一次出现f,并删除其间的所有内容

它是这样做的

"Adaptation" means a work based upon the Work, or cAt upon the Work and other pre-existing works, such as a f translation, DOG adaptation, derivative work, arrangement of music or other alterations of a literary or artistic work, or phonogram f or performance and includes cinematographic adaptations or any


Tags: orandoftheperformancetranslationmeanscat
1条回答
网友
1楼 · 发布于 2024-05-13 22:09:49

*是贪婪的重复:“0或更多,尽可能多”。这使得您的第一个匹配项可以抓取从cAt到最后一个f的所有内容,而不为DOG查询留下任何内容

改用*?:“0或更多,尽可能少”。这样,regexp将满足于只使用cAt和第一个f之间的位,并且DOGregex也将有它的一天

相关问题 更多 >