首页 > 解决方案 > 查找匹配 3 个连续元音正则表达式的单词

问题描述

text = "Life is beautiful"
pattern = r"[aeiou]{3,}"
result = re.findall(pattern, text)
print(result)

期望的结果: ['beautiful']

我得到的输出: ['eau']

我试过谷歌搜索等等......我找到了多个答案,但没有一个有效!我是正则表达式的新手,所以也许我遇到了问题,但我不知道如何解决这个问题

我已经尝试过r"\b[abcde]{3,}\b"仍然没有使用所以请帮忙!

标签: pythonregexstringfindandmodify

解决方案


您的正则表达式仅捕获 3 个连续的元音,因此您需要扩展它以捕获单词的其余部分。这可以通过查找两个分词之间的字母序列并在序列中对 3 个连续元音使用正向前瞻来完成。例如:

import re

text = "Life is beautiful"
pattern = r"\b(?=[a-z]*[aeiou]{3})[a-z]+\b"
result = re.findall(pattern, text, re.I)
print(result)

输出:

['beautiful']

推荐阅读