0

tried:

typosRegex = re.compile(r'(\w* ){2,}')

mo2 = typosRegex.sub(r'\1', 'ram is a good good personalities.')

print(mo2)

result:

good personalities.

expect:

ram is a good personalities.

how to get the expected substituted string using regex? how to replace accidently repeated word with the single of that?

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • 2
    `(\w* ){2,}` does not mean "the same word twice in a row". It means "two in a row of *any word*". So "ram is" and "a good" get substituted out. "good personalities." does not, only because there is no trailing space. – Karl Knechtel Jul 03 '22 at 23:38
  • `re.sub(r'(\w+)\s+\1',r'\1', your_string)` – dawg Jul 03 '22 at 23:44

0 Answers0