How to remove words which contains same character more number of time?

Viewed 86

Here's an example input,,

text = "John Vv"

I need to remove Vv because it's same character repeating multiple times?

If the word contains other characters in it don't remove it, eg VvSh.

I tried something like this, which worked but I don't want to write same line for every english character out there, Any help is appreciated.

re.sub("vv", "", text, flags=re.I)
1 Answers

You can use

re.sub(r"\b([A-Za-z])\1+\b", "", text, flags=re.I)

Details:

  • \b - a word boundary
  • ([A-Za-z]) - Group 1: an ASCII letter
  • \1+ - one or more chars equal to the one captured in Group 1
  • \b - a word boundary

See the regex demo.

Related