Remove repeating characters from words

后端 未结 4 757
醉酒成梦
醉酒成梦 2020-12-09 20:23

I was wondering what is the best way to convert something like \"haaaaapppppyyy\" to \"haappyy\".

Basically, when parsing slang, people sometimes repeat characters

4条回答
  •  囚心锁ツ
    2020-12-09 21:01

    It can be done using regular expressions:

    >>> import re
    >>> re.sub(r'(.)\1+', r'\1\1', "haaaaapppppyyy")     
    'haappyy'
    

    (.)\1+ repleaces any character (.) followed by one or more of the same character (because of the backref \1 it must be the same) by twice the character.

提交回复
热议问题