我正在尝试创建一个函数,从下面的字符串中提取具有3个连续元音的单词
import re
def three_vowel_words(text):
pattern = "[\w]+[aeiou]{3}"
result = re.findall(pattern, text)
return result
print(three_vowel_words("Our team was victorious over theirs in the contest."))
#the output should be ["victorious"]
print(three_vowel_words("Obviously, the acquaintance is serious and ambitious."))
#the output should be ["Obviously","acquaintance","serious","ambitious"]
然而,我得到的结果如下:
["victoriou"]
["Obviou", "acquai", "seriou", "ambitiou"]
有人能帮我纠正我的正则表达式模式,这样它就能提取完整的单词
多谢各位
使
"\w+[aeiou]{3}\w+"
这样单词的其余部分也匹配。在当前解决方案中,当找到3个连续元音时,匹配将停止编辑:正如@usr2564301和@Tomerikoo所指出的,更一般的方法是
"\w*[aeiou]{3}\w*"
。这样,以3个元音开头或结尾或只有3个元音的单词也会匹配相关问题 更多 >
编程相关推荐