简体   繁体   English

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

[英]use regex to find sentences containing duplicated words

I've tried below.我在下面试过。

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('.')])

I am wondering how to print the entire sentence(s) that contain duplicated words.我想知道如何打印包含重复单词的整个句子。

Here is one option, using a list comprehension along with re.search :这是一种选择,使用列表理解和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)

This prints:这打印:

['Glitches happened happened happened.']

Could you please try following, using re.search function of Python's re library.您能否尝试以下操作,使用 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']

As workaround, you can try:作为解决方法,您可以尝试:

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)])

result:结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM