我想要一个正则表达式(在 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."
关于python - 如何在 Python 中使用正则表达式删除单词中的多个后续字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662183/