I faced a problem that I hope to construct a regrex to distinguish a string with even numbers of 'a' and even numbers of 'b':
A plausible solution would be like this: (aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*)* But after checking, a simple string 'a' can also pass this check, but I do not know why...
Moreover, based on the previous condition, I have to also allow 'c' to exist (0 to many) times at any positions. That is, the string should contain even numbers of 'a' and even numbers of 'b' and (0 to many) numbers of 'c'. Only these three characters are allowed.
How may I solve this using only regrex expression?
Thanks a lot for help!
CodePudding user response:
(aa|bb)((ab|ba)(aa|bb)(ab|ba)(aa|bb)) But after checking, a simple string 'a' can also pass this check, but I do not know why...
You probably forgot to anchor the match with ^
$
in order to apply only to the whole string. Anyway, moreover with c
allowed at any position, this approach causes a headache. I suggest to use lookahead assertions for the even numbers restriction:
^(?=[bc]*(a[bc]*a[bc]*)*$)(?=[ac]*(b[ac]*b[ac]*)*$)[abc]*$
(We could replace a[bc]*a[bc]*
by (a[bc]*){2}
etc.)