python - 如何在 Python 中使用正则表达式删除单词中的多个后续字符?

标签 python regex

我想要一个正则表达式(在 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/

相关文章:

python - SQLAlchemy 根据 JSONB 中的嵌套键进行过滤

php - 未知修饰符 'g' PHP 正则表达式错误

javascript - 我想在使用 javascript 正则表达式时忽略方括号

ruby - 匹配 "AAAA:AAA"模式的正则表达式

python - reshape numpy 数组

python - 如何从 python lambda 返回一个 graphql Union 到 AWS appsync?

javascript - Firefox Add-on-SDK page-mod 匹配模式/通配符错误

正则表达式字符串不包含子字符串

python - Google Cloud Storage - 如何通过 Python 检索文件的所有者?

python - 两个数组累积交集的快速算法