[issue1519638] Unmatched Group issue - workaround
report at bugs.python.org
Fri Jul 11 10:17:20 CEST 2008
Gerard <gjp at gp-net.nl> added the comment:
I found a workaround for the re.sub method so it does not raise an
exception but returns and empty string when backref-ing an empty group.
This is the nutshell:
When doing a search and replace with sub, replace the group represented
as optional for a group represented as an alternation with one empty
subexpression. So instead of this “(.+?)?” use this “(|.+?)” (without
the double quotes).
If there’s nothing matched by this group the empty subexpression
matches. Then an empty string is returned instead of a None and the sub
method is executed normally instead of raising the “unmatched group” error.
A complete description is in my post:
title: Unmatched Group issue -> Unmatched Group issue - workaround
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list