I need to replace words like goooooooooooood with good. For this i tried
t.replace(r'(.)\2+',r'\2')
where t is some word like gooooooooooood
but this doesn't work.
What you are looking for is a spell checker. There are multiple ways of doing it but few ways I found useful is
You can use itertools.groupby()
:
In [53]: strs="goooooooooooood"
In [54]: from itertools import groupby
In [55]: "".join(k*2 if len(list(g))>=2 else k for k,g in groupby(strs))
Out[55]: 'good'
Regex solution
import re
s = "goooooooooooooood"
print re.sub(r'(.)\1{2,}', r'\1', s)
or
print re.sub(r'(.)\1{3,}', r'\1\1', s)
To replace gooooooooooood with good
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.