Reputation:
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:
Upvotes: 4
Views: 4776
Reputation: 7
I tried this using help from sniperd's answer:
def multi_vowel_words(text):
pattern = r"\w+[aeiou]\w*[aeiou]\w*[aeiou]\w+"
result = re.findall(pattern, text)
return result
This works even with uppercases.
If you have numbers and underscore in your text, then instead of \w
use [a-zA-Z]
.
Upvotes: 0
Reputation: 5274
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! :)
Upvotes: 5
Reputation: 2812
just
(\w*[aeuio]\w*){3,}
or if you want line match
^(.*[aeuio].*){3,}$
Upvotes: 4
Reputation: 521514
Try this pattern:
^.*[AEIOUaeiou].*[AEIOUaeiou].*[AEIOUaeiou].*$
We could also use a positive lookahead:
^(?=.*[AEIOUaeiou].*[AEIOUaeiou].*[AEIOUaeiou]).*$
Note that due to the possibility of backtracking I would probably prefer using the first (non lookahead) pattern because it should be more efficient.
Upvotes: 0
Reputation: 2738
This can be achieved using lookaheads like this.
Regex: ^(?=.*[aeiou].*[aeiou].*[aeiou])(?:[a-z] *)+$
Explanation:
(?=.*[aeiou].*[aeiou].*[aeiou])
positive lookahead checks for presence of any character followed by vowel three times.
(?:[a-zA-Z] *)+
matches your one or more English words separated by spaces.
If Case insensitive Mode is OFF use following regex
Regex: ^(?=.*[aeiouAEIOU].*[aeiouAEIOU].*[aeiouAEIOU])(?:[a-zA-Z] *)+$
Upvotes: 0