python - Replace sequence of same characters -
what fastest way in python replace sequence of 3 , more same characters in utf-8 text?i need replace sequence of 3 , more same characters exact 2 characters. i.e.
aaa -> aa bbbb -> bb abbbcd -> abbcd 124xyyyz3 -> 124xyyz3
>>> import re >>> re.sub(r'(\w)\1{2,}', r'\1\1', 'aaa') 'aa' >>> re.sub(r'(\w)\1{2,}', r'\1\1', 'bbbb') 'bb'
Comments
Post a Comment