0

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.

5
  • 3
    Does not make sense, why should goooooooooooood be good and not god? Commented Nov 11, 2012 at 15:54
  • t seems to be a string, that means you're not using regex but regular character replacement here. Commented Nov 11, 2012 at 15:58
  • when a user types in gooooood, he's most likely referring to good and not god eg: the burger here was goooooooooooood Commented Nov 11, 2012 at 16:09
  • 1
    And what is the expected result for baaaaad? Commented Nov 11, 2012 at 16:31
  • @user1747696: How is the program supposed to know which to use, one or two? Commented Nov 11, 2012 at 16:49

3 Answers 3

3

What you are looking for is a spell checker. There are multiple ways of doing it but few ways I found useful is

Sign up to request clarification or add additional context in comments.

Comments

2

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'

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.