我想要一个正则表达式(在Python中)给出如下句子:
heyy how are youuuuu, it's so cool here, cooool.
将其转换为:
heyy how are youu, it's so cool here, cool.
表示一个字符可以重复最多1次,如果超过该值则应该删除。
heyy ==> heyy
youuuu ==> youu
cooool ==> cool
答案 0 :(得分:1)
您可以在模式中使用反向引用来匹配重复的字符,然后将其替换为匹配字符的两个实例,此处(.)\1+
将匹配包含相同字符两次或更多次的模式,将其替换为\1\1
只有两个实例:
import re
re.sub(r"(.)\1+", r"\1\1", s)
# "heyy how are youu, it's so cool here, cool."
答案 1 :(得分:0)
创建一个新的空文本,如果没有连续3个
,则只添加它text = "heyy how are youuuuu, it's so cool here, cooool."
new_text = ''
for i in range(len(text)):
try:
if text[i]==text[i+1]==text[i+2]:
pass
else:
new_text+=text[i]
except:
new_text+=text[i]
print new_text
>>>heyy how are youu, it's so cool here, cool.
eta:嗯,刚刚注意到你要求“正则表达式”,所以批准的答案更好;虽然这有效