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']
帮助我用正确的模式获得所需的输出
我会保持简单,并在不区分大小写的模式下匹配模式bw*[aeiou]{3}w*b
:
def multi_vowel_words(text):
return re.findall(r'bw*[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']
您可以更轻松地捕获模式前后的字母。
pattern = r'(w*.[a,e,i,o,u]{2,}w*)'
此正则表达式模式r"\b[\w][aeou]{3,}[\w]">将匹配具有3个或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']