如何删除任何Unicode重复字母?

时间:2019-05-28 08:03:43

标签: python string unicode

在英语中,有时您会重复这样的字母: hello my hero hhhhhhhhhhh用于h,但是我想删除所有重复两次或两次以上的各种字母,并用Unicode字母空格替换。我这里有阿拉伯语。我只有一个字母可以删除,这是我的代码:

#remove laughing
def remove_laughs(self, text):       
    text=re.sub("ه{2,}", "", text)
    return text

2 个答案:

答案 0 :(得分:0)

尝试一下:

from itertools import groupby

def remove_dups(s):
    replace_with = ' '
    return ''.join([x if sum(1 for i in y)<2 else replace_with for x,y in groupby(s)])

答案 1 :(得分:0)

任何重复的字符

import re
re.sub(r'(.)\1+', ' ', 'مرحبا هههههههههه')
# 'مرحبا  '

仅字母字符

import regex
regex.sub(r'(\pL)\1+', ' ', 'مرحبا هههههههههه')