multi_元音_words函数返回所有具有3个或更多连续元音的单词

2024-06-17 10:28:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我做错了什么

运行代码后,我得到以下信息:

[]
['queen is courageous and gracious']
['quietly and await their delicious dinner']
[]
[]

#below this are desired outputs
print(multi_vowel_words("Life is beautiful")) 
# ['beautiful']

print(multi_vowel_words("Obviously, the queen is courageous and 
gracious.")) 

# ['Obviously', 'queen', 'courageous', 'gracious']

print(multi_vowel_words("The rambunctious children had to sit quietly and 
await their delicious 
dinner.")) 
# ['rambunctious', 'quietly', 'delicious']

print(multi_vowel_words("The order of a data queue is First In First Out 
(FIFO)")) 
   # ['queue']

print(multi_vowel_words("Hello world!")) 
   # []
print(multi_vowel_words("The order of a data queue is First In First Out 

(FIFO)")) # ['queue']

print(multi_vowel_words("Hello world!")) # []

Tags: andtherequeueismultifirstwords
3条回答

每个人都在使用\w来匹配元音的前后字符。这是错误的,因为\w匹配任何单个字母、数字或下划线(与[a-zA-Z0-9_]相同)。正确的模式应该是:

pattern = r"\b[A-Za-z]*[aeiou]{3,}[A-Za-z]*\b"
pattern = r"\w*[aeiou]{3,}\w*"

这会奏效的

试图尝试回答这个问题。用一个简单的模式检查三个连续的元音怎么样

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

相关问题 更多 >