Reputation: 6908
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?
Upvotes: 0
Views: 94
Reputation: 24052
The problem is the group at the start of the pattern is required to match. You can fix it by making it optional (adding a ?
after it):
>>> re.match(r"(?P<x>\d)?(?(x)a|b)", "5a")
<_sre.SRE_Match object; span=(0, 2), match='5a'>
>>>
>>> re.match(r"(?P<x>\d)?(?(x)a|b)", "b")
<_sre.SRE_Match object; span=(0, 1), match='b'>
>>>
Upvotes: 1