The problem
I'm trying to create a regex in which we can check if all letters present in some reference set are present in some other string, but only in odd numbers (1, 3, 5, ...).
Here is a (very) crude image of the DFA representing the problem:
My (broken) solution
I started using a finite set, {a, b}
, so I would essentially check "are there both an odd number of a
s and an odd number of b
s in the string?"
Unfortunately I did not get far on my own. I first read this thread, which is remarkably similar to this concept, but was not able to glean an answer from (aa|bb|(ab|ba)(aa|bb)*(ba|ab))*(b|(ab|ba)(bb|aa)*a)
. (I understand how it works, but not how to convert it to check odd numbers for both items present.)
Here is what I've come up with so far: ^((ab|ba)(bb|aa)?|(bb|aa)?(ab|ba))+$
. This basically checks if there is ab
or ba
followed by bb
or aa
or nothing, which would result in ab
, ba
, abaa
, abbb
, baaa
, or babb
. (It also does the reverse of this, checking the double-letter first.) This can then repeat, indefinitely. The problem I have is I cannot seem to adjust it to match the string bbaaba
without also matching bbaa
.
Additionally, the method above can not be dynamically adjusted to account for {a, b, c}
, for example, though I'm willing to forgo this to solve the initial problem.
Testing
Here are my test strings and the desired output, with the reasons in parentheses:
"ba" # True (1a, 1b)
"abbb" # True (1a, 3b)
"bbba" # True (1a, 3b)
"bbab" # True (1a, 3b)
"ababab" # True (3a, 3b)
"bbaaba" # True (3a, 3b)
"abb" # False (2b)
"aabb" # False (2a, 2b)
"aabba" # False (2b)
"" # False (0a, 0b is "even")
"a" # False (0b is "even")
"b" # False (0a is "even")
Question
So, is this possible through regex? Or are regular expressions more limited than a DFA? I am aware that it can be done through a basic loop, but this isn't what I'm going for.