开发者

Grammar - RegEx - containing five vowels (aeiou)

开发者 https://www.devze.com 2023-04-09 19:38 出处:网络
I am trying to learn regular expression. I have L = {a, b, x, y, z, i, o, u, e, c} I want to construct a regular expression that describes a strings that contain the five vowels in alphabetical or

I am trying to learn regular expression. I have

L = {a, b, x, y, z, i, o, u, e, c}

I want to construct a regular expression that describes a strings that contain the five vowels in alphabetical order (aeiou)开发者_StackOverflow社区. All strings will have at least one of all five vowels.

Do I have to lay them out in order as they are in the set? like

a(b*x*y*z*i*o*u*ec*)iou

or can I mix them up like:

aeiou(b*x*y*z*c*)

Since, they are not in order in the set, does that mean the first solution is what I am looking for?


In most regex languages, you'll need something like:

[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*

That much is essentially uniform. You then have to deal with 'start of word' and 'end of word' issues, which depend on the context and the regex language. With one word per line, you can simply use '^' to start and '$'.


Using your preferred notation and knowing that the complete alphabet used consists of the 10 letters, and assuming you can do grouping, then you can write:

(b*c*x*y*z*)*a(b*c*x*y*z*)*e(b*c*x*y*z*)*i(b*c*x*y*z*)*i(b*c*x*y*z*)*u(b*c*x*y*z*)*

The (b*c*x*y*z*)* part says zero or more repeats of "zero or more b's followed by zero or more c's, ..., followed by zero or more z's". This does what you require; but it also demonstrates why character class notation is such a good idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消