Python re.sub to replace 3 or more of same character with 1

后端 未结 3 829
耶瑟儿~
耶瑟儿~ 2021-01-06 15:15

I\'m trying to find characters that are repeated 3 times or more, for example I want to take the following strings:

(\'aaa\', \'buuuuut\',

3条回答
  •  -上瘾入骨i
    2021-01-06 15:52

    Look at this:

    >>> mystr = 'buuuuuttttt'
    >>> re.sub(r'(.)\1{2,}', r'\1', mystr)
    'but'
    >>> mystr = 'buttt'
    >>> re.sub(r'(.)\1{2,}', r'\1', mystr)
    'but'
    >>>
    

提交回复
热议问题