regex multi_vowel_words函数返回所有包含3个或更多连续元音(a,e,i,o,u)的单词,填充正则表达式来完成此操作

uoifb46i  于 2023-06-25  发布在  其他
关注(0)|答案(3)|浏览(92)
import re
def multi_vowel_words(text):
  pattern = r"\b(?=[a-z]*[aeiou]{3})[a-z]+\b"
  result = re.findall(pattern, text)
  return result
print(multi_vowel_words("Obviously, the queen is courageous and gracious."))
# Should be ['Obviously', 'queen', 'courageous', 'gracious']

我得到的输出:-['queen', 'courageous', 'gracious']帮助我得到所需的输出与正确的模式

34gzjxbg

34gzjxbg1#

我会保持简单,并在大小写不敏感模式下匹配\b\w*[aeiou]{3}\w*\b模式:

def multi_vowel_words(text):
    return re.findall(r'\b\w*[aeiou]{3}\w*\b', text, flags=re.IGNORECASE)

inp = "Obviously, the queen is courageous and gracious."
words = multi_vowel_words(inp)
print(words)  # ['Obviously', 'queen', 'courageous', 'gracious']
2izufjch

2izufjch2#

您可以以更简单的方式捕捉图案前后的字母。

pattern = r'(\w*.[a,e,i,o,u]{2,}\w*)'
z2acfund

z2acfund3#

此正则表达式模式**r”\B[\w][aeiou]{3,}[\w]"将匹配具有3个或更多连续元音{3,}的单词,包括以大写字母\b[\w]***开头的单词

import re

def multi_vowel_words(text):
  pattern = r"\b[\w]*[aeiou]{3,}[\w]*"
  result = re.findall(pattern, text)
  return result

print(multi_vowel_words("Obviously, the queen is courageous and gracious.")) 
# ['Obviously', 'queen', 'courageous', 'gracious']

相关问题