我想要一个正则表达式(在 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
请您参考如下方法:
您可以在模式中使用反向引用来匹配重复的字符,然后将其替换为匹配字符的两个实例,这里 (.)\1+ 将匹配包含相同字符的模式两次或更多次,通过 \1\1 仅用两个实例替换它:
import re
re.sub(r"(.)\1+", r"\1\1", s)
# "heyy how are youu, it's so cool here, cool."
