Grammars - RegEx
I am trying to construct a regular expression that the total number of a's is divisible by 3 no matter how they are distributed. aabaabbaba. This is What i came up with:
b*ab*ab*
Now, someone told me i could do it this way
(b*ab*ab*)*
Why would i need to enclose it and why is the outside kleene star needed?
Wouldnt the outside kleene distribute among all the a's a开发者_Python百科nd b's inside the parenthesis? if thats the case then what would a double kleene mean?
For the number of 'a's to be divisible by three, you'll need three 'a's in your expression. So the correct expression is:
(b*ab*ab*ab*)*
This expression is saying 'a' three times, with possible 'b's in the middle. The last star says repeat (the whole parenthesized expression) as necessary.
The outer *
repeats the entire sequence zero or more times.
In other words, zero or more substrings that match b*ab*ab*
.
精彩评论