regex to find vowels

davextreme

New member
Joined
Feb 14, 2005
Messages
1
Im trying to find a regex that will verify that a string has all 5 vowels in it [a,e,i,o,u]. I dont care what order theyre in or if they appear twice. This would be easy enough to do programmatically but Im wondering if one nice regex can get it all at once?
 
Complex

davextreme said:
Im trying to find a regex that will verify that a string has all 5 vowels in it [a,e,i,o,u]. I dont care what order theyre in or if they appear twice. This would be easy enough to do programmatically but Im wondering if one nice regex can get it all at once?

Im still looking into your question, but at first pass my answer is that it will be difficult to find a regular expression that will find all five vowels in any order with one expression. There are 5 vowels, aeiou, and if you arrange them in all possible orders that would be a permutation of 5 things taken 5 at a time. That amounts to 120 possible unique orderings of the 5 vowels. You could use the expression below:

[aeiou].*[aeiou].*[aeiou].*[aeiou].*[aeiou]

but that will also find "aaaaa", "aaaoo", and so forth. You could use the one below:

[a].*[e].*.*[o].*

but that finds one unique permutation, and there are 120 unique permutations. Right now I cant think of one regular expression that will find all five vowels in any order. If I think of or stumble across one I will reply. Perhaps someone else has somethoughts on this? :confused:
 
?maybe do it the long way?

davextreme said:
Im trying to find a regex that will verify that a string has all 5 vowels in it [a,e,i,o,u]. I dont care what order theyre in or if they appear twice. This would be easy enough to do programmatically but Im wondering if one nice regex can get it all at once?

might be easiest to do five seperate regexs. pseudoCode:
Code:
\s([^aeiou\s]*[a])\s
_foreach match{
   \s([^aeiou\s]*[e])\s
   _foreach match{
      \s([^aeiou\s]*[i])\s
      _foreach match{
         \s([^aeiou\s]*[o])\s
         _foreach match{
             \s([^aeiou\s]*[u])\s
             if match ALLVOWELS=1
         }
      }
   }
   if ALLVOWELS=1
   //your code goes here
}
Apparently the cost is really high if you build the regexs and matches within the foreach loops, so you might need to pre-build them before this nested loop. If thats the case it gets a little messy resetting each match object (and regex?).

but the above post is right, the best/most efficient way to run this is to write every permutation and union the result ... use that as the string. This regex itself could be generated with code.
 
.NET to the rescue!?

seve7_was response got me to thinking. I investigated the .NET regex documentation. I came up with the following one line piece of code below that (I think) does what you want:

Code:
bool vowel_check = Regex.IsMatch(input_string, "a") && Regex.IsMatch(input_string, "e") && Regex.IsMatch(input_string, "i") && Regex.IsMatch(input_string, "o") && Regex.IsMatch(input_string, "u")
The above statement (in C#, modify for your language of choice) will return true only if all the vowels are found in the input_string. It takes the five IsMatch boolean return values and "ands" them together. Only if all are true will the result be true.

Try it out and let us know how it works. :)
 
hey Richard, I like that :)

With your idea, youd start by splitting the string into individual words (like split on \s+), then iterate through each word (?) Its wonderfully simple and appears efficient... the only other thing I think might also be nice is to rearrange the vowels from least to most frequent -- if thats an easy fact to find.
 
Regex!!

seve7_wa said:
hey Richard, I like that :)

With your idea, youd start by splitting the string into individual words (like split on \s+), then iterate through each word (?) Its wonderfully simple and appears efficient... the only other thing I think might also be nice is to rearrange the vowels from least to most frequent -- if thats an easy fact to find.

Yes, if you have a string and you want to check each word then you would have to parse the words out to check them. And to determine vowel frequency maybe we could check the values in the Scrabble game? :p

I appreciate everyones posts in this regex forum. I have learned a lot about .NET regex while trying to find answers to questions posted. :)
 
Hey, uh, HJB417, an explaination is always nice...

[^\s]+[aeiou]+[^\s]+

Im not very good with regular expressions, and Im not sure what that is. Non-whitespace char(s), followed by vowel(s), followed by non-whitespace char(s)? Am I correct...? If so, what is it supposed to find?


If you are looking to find all the words, wouldnt this work? \s\S+\s Non whitespace surrounded by whitespace?
 
The regex is self explanatory, but Ill try to put it into layman terms and because youre making me explain it, I found a bug in my regex so heres a revised regex

\w*[aeiou]+\w*

laymans terms

\w = [a-zA-Z0-9]

so find me text where the beginning of the string is 0 or more characters, followed by a vowel, followed by zero or more characters.
 
i know im overreacting but...

Oh, gee, thanks for dumbing it down for a moron like me. I just noticed that more than once you have given a reply in regex that is not necessarily self explanatory to those who did not write it and especially those not particularly familiar with regex, like me. I know what \w is, thanks. I know what * is. I know what + is. I know what [aeiou] does. I can put the pieces together too and disect them as well, but it is not necessarily clear to everyone, especially the layman, what a pattern as a whole might return for matches. A one line explaination of what you are posting is too much to ask for?

I personally prefer to explain all of my code that somebody might not find self explainatory, just so more people can benefit from my answer, and I certainly do not do so in a condescending or insulting manner.

Please, play nice with those like me who are not gurus like you. Im here to learn and try to help, not to have people complain at me cause they have to dumb down their "self explanatory" regex pattern. I am new to regex, and do not have a comprehensive understanding of it. Sorry. But, after all, thats why Im here.
 
marble_eater said:
Oh, gee, thanks for dumbing it down for a moron like me. I just noticed that more than once you have given a reply in regex that is not necessarily self explanatory to those who did not write it and especially those not particularly familiar with regex, like me. I know what \w is, thanks. I know what * is. I know what + is. I know what [aeiou] does. I can put the pieces together too and disect them as well, but it is not necessarily clear to everyone, especially the layman, what a pattern as a whole might return for matches. A one line explaination of what you are posting is too much to ask for?

I personally prefer to explain all of my code that somebody might not find self explainatory, just so more people can benefit from my answer, and I certainly do not do so in a condescending or insulting manner.

Please, play nice with those like me who are not gurus like you. Im here to learn and try to help, not to have people complain at me cause they have to dumb down their "self explanatory" regex pattern. I am new to regex, and do not have a comprehensive understanding of it. Sorry. But, after all, thats why Im here.

Im by no means a guru, no one was born into this world with the innante knowledge of regex, and it is not some arcane language. Up until a about 3 months ago, I had little understanding of regex, except what I was taught in theory of computation but a $30 investment quickly changed that.
 
Back
Top