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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -