Regex match ordering

只谈情不闲聊 提交于 2020-12-31 05:29:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!