An general "odd" count for "x" is (([^x]*x){2})*[^x]*x[^x]*
. Use two look aheads with this kind of expression to assert that both characters 1 and 0 are present an odd number of times:
^(?=(0*10*1)*0*10*$)(?=(1*01*0)*1*01*$).*
See live demo.
The regex is basically 2 look aheads, which have the form (?=...)
(where ...
is an expression) and assert (without consuming) that the input following matches the expression. Because it doesn't consume the input, it doesn't move the pointer, so you can have multiple look aheads at the same point.