Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex string with 3 or more vowels

Tags:

regex

I'm trying to make a regular expression that matches a String with 3 or more vowels.

I've tried this one:

[aeiou]{3,}

But it only works when the vowels are in a sequence. Any tips ?

For example:

  1. Samuel -> valid
  2. Joan -> invalid
  3. Sol Manuel -> valid
  4. Sol -> Invalid

1 Answers

There are several ways to do it and in this case keeping it simple will probably be the most helpful to future devs maintaining that code. That's a fun part about regexes, you can make them very efficient and clever and then very hard for somebody who doesn't do them often to update.

import re

regex = "[aeiou].*[aeiou].*[aeiou]"

mylist = [
"Samuel",  #yes!
"JOAN",  #no!
"Sol Manuel",  #yes!
"",  #no!
]

for text in mylist:
    if re.search(regex, text, re.IGNORECASE):
        print ("Winner!")
    else:
        print ("Nein!")

You could also adjust each part to be [aeiouAEIOU] if you don't have an ignore case flag in your language of choice. Good luck! :)

like image 172
sniperd Avatar answered Sep 17 '25 20:09

sniperd