BalaYogi
BalaYogi

Reputation: 11

The multi_vowel_words function returns all words with 3 or more consecutive vowels (a, e, i, o, u). Fill in the regular expression to do that

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

Answers (3)

Paul Yonga
Paul Yonga

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

ebaum
ebaum

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

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions