import re
class RepeatReplacer(object):
""" 在该方法中,一个字符指的是正则表达式分组中的先前字符。消除重复字符也被认为是标准化任务之一。 RepeatReplacer类通过编译正则表达式和替换的字符串来工作,并使用 backreference.Repeat_regexp 来定义。 它匹配可能是以零个或多个(\w *)字符开始,以零个或多个(\w *),或者一个(\w)其后面带有相同字符的字符而结束的字符。 """
def __init__(self):
self.repeat_regexp = re.compile(r'(\w*)(\w)\2(\w*)\3')
self.repl = r'\1\2\3'
def replace(self, word):
repl_word = self.repeat_regexp.sub(self.repl, word)
print('*'*10)
print(repl_word)
if repl_word != word:
return self.replace(repl_word)
else:
return repl_word
replacer = RepeatReplacer()
print(replacer.replace('oohhhh'))
print(replacer.replace('hottt'))