I'm having trouble getting a regex to work using a named group and a conditional expression. I've simplified the problem to the smallest possible example.
The regex r"(?P<x>\d)(?(x)a|b)" would seem to mean "match a digit, and if you matched a digit (named group x) also match an a, but if not, match b". This should be equivalent to r"(\da|b)".
However, while it matches for the yes clause, it doesn't match the no clause:
>>> re.match(r"(?P<x>\d)(?(x)a|b)", "5a")
<re.Match object; span=(0, 2), match='5a'>
>>> re.match(r"(?P<x>\d)(?(x)a|b)", "b")
>>>
It's also not working with a numbered group:
>>> re.match(r"(\d)(?(1)a|b)", "5a")
<re.Match object; span=(0, 2), match='5a'>
>>> re.match(r"(\d)(?(1)a|b)", "b")
>>>
What am I missing?