Ahtsham
Ahtsham

Reputation: 25

Regex for strings that contain only odd clusters of b's with set {a,b}

This must be valid

ababbba

This must be invalid

bb

I only know that this regex ensures odd b's per iteration

b(bb)*

However my current regex fails when repeated.

(a*b(bb)*)*

Upvotes: 1

Views: 107

Answers (1)

hair raisin
hair raisin

Reputation: 2628

The problem with the expression (a*b(bb)*)* is that the a* can match 0 or more a, so it would match abbb[0 a]b. I think what you want is something like this:

^a*b(bb)*(a+b(bb)*)*a*$

that would match any number of a, then an odd number of b followed by 0 or more odd numbers of b, as long as there is one or more a in between, then any number of a at the end. I included the start and end terminators because regex coach gives me partial matches without them.

Upvotes: 2

Related Questions