Reputation: 11
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']
the output I got:-
['queen', 'courageous', 'gracious']
help me to Get the desired output with the correct pattern
Upvotes: 0
Views: 1981
Reputation: 21
This regex pattern r"\b[\w][aeiou]{3,}[\w]" will match words with 3 or more consecutive vowels {3,} , including word starting with capital letters \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']
Upvotes: 1
Reputation: 21
You can capture the letters before and after the pattern in an easier way.
pattern = r'(\w*.[a,e,i,o,u]{2,}\w*)'
Upvotes: 2
Reputation: 521987
I would keep it simple and match on the pattern \b\w*[aeiou]{3}\w*\b
in case insensitive mode:
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']
Upvotes: 3