为包含至少一个a和至少一个b的字母{a,b,c)的字符串集写一个正则表达式表达式

时间:2015-08-22 17:41:45

标签: theory formal-languages computation

为包含至少一个a和至少一个b的字母{a,b,c}的字符串集写一个正则表达式表达式

我想回答这个问题,

任何人都可以帮助我吗?

由于

欢乐

3 个答案:

答案 0 :(得分:2)

E*(aE*b)|(bE*a)E*

E是西格玛,所以字母表中的任何字符

答案 1 :(得分:0)

我没有足够的评论来评论joeya17的答案,但我不知道Regex会如何接受字符串“abc”或“cba”。

我最初想出了这个正则表达式:

( a+b+c* U a+c*b+ U b+a+c* U b+c*a+ U c*+a+b+ U c*b+a+ )+

我觉得应该有用,但是看到joeya17的回答,我想你也可以这样做:

E = any letter in Sigma
(E*a+E*b+|a+E*b+E*|b+E*a+E*|E*b+E*a+)+

答案 2 :(得分:-1)

For the regular expression there are various possibilities:-
Now,
exactly one a and one b (where a comes before b) - c* a c* b c*
exactly one a and one b (where b comes before a) - c* b c* a c*
We take this idea to the answer for more than one a and b:-
a comes before b
(c* a a* c* b b* c*) ;
b comes before a
(c* b b* c* a a* c*)
or there are intermmediate occurances of a and b :-
Let RE R=(a+b+c)* 
this is all possible occurences of a,b,c in all orders ;
Hence when a comes before b;
(R* a R* b R*);
when b comes before a;
(R* b R* a B*)

we take the union to get the final answer ( R* a R* b R* + R* b R* a R*) where R = (a + b + c)*