Theoretically, the language in the question is context-free, so it cannot be described with a regular expression. However, regular expression engine in the languages and library introduces features which make them more powerful than theoretical regular expression.
In this case, it is simply asking you to detect palindrome string, except that the total length of the string is always even.
We can take the regex from this answer and modify it:
(?<N>[01])+(?<-N>\k<N>)+(?(N)(?!))
I dropped the .?
part, which allows for odd-length palindrome, and change the .
inside (?<N>.)
to fit the question)
(?<N>[01])+
captures and pushes the character into "stack" N
.
(?<-N>\k<N>)+
pops from "stack" N
(note the <-N>
) when the item on top of the stack matches the current character (\k<N>
, this is the usual back-reference to capturing group N).
(?(N)(?!))
checks that if there is anything left on the stack N
, then fail and backtrack.
Note that this regex is unique to .NET engine due to balancing group feature in .NET. Perl and PCRE can also match the language in the question, but with different feature (routine call).