开发者

Grammars - RegEx

开发者 https://www.devze.com 2023-04-10 15:54 出处:网络
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:

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*.

0

精彩评论

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