I want a regular expression (in Python) that given a sentence like:
heyy how are youuuuu, it's so cool here, cooool.
converts it to:
heyy how are youu, it's so cool here, cool.
which means maximum of 1 time a character can be repeated and if it's more than that it should be removed.
heyy ==> heyy
youuuu ==> youu
cooool ==> cool
You can use back reference in the pattern to match repeated characters and then replace it with two instances of the matched character, here (.)\1+ will match a pattern that contains the same character two or more times, replace it with only two instances by \1\1:
import re
re.sub(r"(.)\1+", r"\1\1", s)
# "heyy how are youu, it's so cool here, cool."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With