使用正则表达式查找包含重复单词的句子

3
我尝试过以下方法。
import re
sentences = "Glitches happened happened happened. Things go back to normal again."
print([re.findall(r"(\w+)\s\1", s) for s in sentences.split('.')])

我想知道如何打印包含重复单词的整个句子(们)。

1
那么你的问题是如何使正则表达式匹配_sentence_? - John Gordon
3个回答

2

这里有一个选项,使用列表推导和re.search

inp = "Glitches happened happened happened. Things go back to normal again."
sentences = re.split(r'(?<=\.)\s+', inp)
duplicates = [s for s in sentences if re.search(r'\b(\S+)\b(?=.*\b\1\b)', s)]
print(duplicates)

这将打印:

['Glitches happened happened happened.']

谢谢。这正是我在寻找的! - rky

2
请尝试使用Python的re库中的re.search函数,如下所示。
>>> import re
>>> sentences = "Glitches happened happened happened. Things go back to normal again."
>>> print ( [el for el in sentences.split('. ') if re.search(r'\b(\w+)\s+\1\b', el)] )
    ['Glitches happened happened happened']

1
完美。谢谢! - rky

2
作为解决方法,您可以尝试以下操作:
import re

sentences = "Glitches happened happened happened. Things go back to normal again. And once again again again."
print([s for s in sentences.split('.') if re.search(r"\b(\w+)\s+\1\b", s)])

结果:

['Glitches happened happened happened', ' And once again again again']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接