I have the below regular expression in Python,
^1?$|^(11+?)\1+$
Since there is a pipe ‘|’, I will split it into 2 regex,
^1?$
For this, it should validate 1 or empty value. Am I correct?
^(11+?)\1+$
For the above regex, it would validate value of 1111. The first pair of 11 is based on (11+?) and the second pair of 11 is due to \1.
When I attempt to execute it in Python, it returns true only for 1111 but not 11 or empty value. Am I wrong somewhere?
Yes, that is correct.
The empty string does get matched. The following snippet:
will print:
And the input
11is not matched because11is matched in group 1 ((11+?)), which should then be repeated at least once (\1+), which is not the case (it is not repeated).