Search code examples
pythonregexstringfindandmodify

find words that matches the 3 consecutive vowels Regex


text = "Life is beautiful"
pattern = r"[aeiou]{3,}"
result = re.findall(pattern, text)
print(result)

desired result: ['beautiful']

the output I get: ['eau']

I have tried googling and etc....I found multiple answers but none of them worked!! I am new to regex so maybe I am having issues but I am not sure how to get this to out

I have tried using r"\b[abcde]{3,}\b" still nothing SO please help!!


Solution

  • Your regex only captures the 3 consecutive vowels, so you need to expand it to capture the rest of the word. This can be done by looking for a sequence of letters between two word breaks and using a positive lookahead for 3 consecutive vowels within the sequence. For example:

    import re
    
    text = "Life is beautiful"
    pattern = r"\b(?=[a-z]*[aeiou]{3})[a-z]+\b"
    result = re.findall(pattern, text, re.I)
    print(result)
    

    Output:

    ['beautiful']