i am searching for a regex for a string containing ‘a’ and ‘b’ that has the following two attributes:
1: The String has an Even number of characters
2: The String may not contain ‘aa’
i am searching for a regex for a string containing ‘a’ and ‘b’ that
Share
It can be easily done with Perl-compatible regular expressions:
^(ab|bb|(ba(?!a)))*$Basically it says that string must consist of
ab,bb,basubstrings mixed in any order, BUTbacannot follow anotheracharacter.The string will have even length because all these subexpressions have even length.
aacan’t appear in a string, because the only way for it to appear is in substringbaab, but the regex specifically restricts ba to be followed bya.