Python 正则表达式:检测包含重复字符的单词

Python regex : detect a word with duplicated chars

我想知道我是否可以检测到“hey”这个词,但是以这种形式:“heeeey” 我将如何在 python 中做到这一点? 我搜索了很多次,但我没有找到答案。

*我希望程序最终像 'hey' 一样,我希望它理解 'heeey' 意味着 'hey'。

import re
p = re.compile(r'(.)+')
test_str = "heeeey"
subst = r""

result = re.sub(p, subst, test_str)

你可以通过 re.sub 来完成。这里我们捕获前面重复的任何 character 并用 替换所有。所以所有重复都将消失。

查看演示。

https://regex101.com/r/cK4iV0/7#python