问题
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?
回答1:
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.)
回答2:
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.
回答3:
You mean in alphabetical order?
You can't do that with a single regex unfortunately. Instead use one regex for each vowel.
来源:https://stackoverflow.com/questions/6066799/regex-match-ordering