use regex to find sentences containing duplicated words

Viewed 66

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.

3 Answers

Here is one option, using a list comprehension along with 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.

>>> 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']
Related