python - 查找匹配 3 个连续元音正则表达式的单词
问题描述
text = "Life is beautiful"
pattern = r"[aeiou]{3,}"
result = re.findall(pattern, text)
print(result)
期望的结果:
['beautiful']
我得到的输出:
['eau']
我试过谷歌搜索等等......我找到了多个答案,但没有一个有效!我是正则表达式的新手,所以也许我遇到了问题,但我不知道如何解决这个问题
我已经尝试过r"\b[abcde]{3,}\b"
仍然没有使用所以请帮忙!
解决方案
您的正则表达式仅捕获 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']
推荐阅读
- javascript - 用 createSound() 创建声音;没有错误
- wpf - wpf devexpress gridcontrol 与单选按钮,将 IsEnabled 绑定传递给 celltemplate
- html - 属性文件中带有 Thymeleaf -UTF-8 的 Spring Boot CRUD 应用程序
- java - 将依赖于迭代器的方法更改为依赖于 ArrayList 或任何替代 (Java)
- karate - 如何使用空手道中的浏览器缓存在 chrome 中运行测试场景?
- django - Django 搜索向量 - 完全关键字不匹配
- java - 在 Spring 应用程序中发送 zip 文件作为多部分表单请求的一部分
- json - 如何使用 Powershell 从 URL 加载 JSON?
- php - 无效反馈未出现(服务器端)(引导程序 4)
- python - 请求目录的 Python 可执行文件