4

My question is simple suppose I would like to match the vowels in a word, but I would like to match them in a specific order as the appear such as a, e, i, o, u. How would I go about doing this?

durron597
  • 31,968
  • 17
  • 99
  • 158
Steffan Harris
  • 9,106
  • 30
  • 75
  • 101

2 Answers2

10

So you're looking for a followed by some characters, then e followed by some characters, and so forth?

In other words, a followed by stuff that isn't e, then e. Then stuff that isn't i then i. Then stuff that isn't o then o. And finally stuff that isn't u and lastly a u.

In regexp terms, that's a[^e]*e[^i]*i[^o]*o[^u]*u

(You could get by with a .*? but why do that when you can more precisely define what you mean.)

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • 1
    You forgot some *s in there, but I think anyone would udnerstand. – Seth Robertson May 20 '11 at 02:16
  • 4
    a slight refinement would be: a[^eiou]*e[^aiou]*i[^aeou]*o[^aeiu]u – Alvin May 20 '11 at 02:26
  • So I did! Added the missing *s in. – VoteyDisciple May 20 '11 at 02:28
  • @Alvin raises an excellent point. I have assumed that any string is valid as long as the vowels exist in that order. So uAuEuIuOuUu is valid, since I can spot (and have capitalized) an A-E-I-O-U sequence, despite an abundance of extra U's. If you need to allow vowels ONLY in that order, you would need to do as Alvin suggests. – VoteyDisciple May 20 '11 at 02:32
  • @Votey your regular expressions doesn't match uAuEuluOuUu but it does match "sacrilegious' which proves your point. – Steffan Harris May 20 '11 at 03:15
  • With `/i` it will. (-: The caps were there for readability, rather than to actually distinguish them from lowercase letters. But quite right — "sacrilegious" would also match. – VoteyDisciple May 20 '11 at 12:25
0

I would go with:

a.*?e.*?i.*?o.*?u

But this has the same problem that is pointed out in a comment by Alvin to Votley's answer. This is due to the question not specified enough. It is not specified what the priority is.

sawa
  • 165,429
  • 45
  • 277
  • 381