Possible regex match bug (re module)

Randall Hopper aa8vb at vislab.epa.gov
Mon Apr 5 08:48:19 EDT 1999


     Re doesn't handle named groups in alternative patterns like it seems
it should.  Given an alternative pattern with a particular group name in
each, it only assigns the match if the group name matches the last
alterative.

For example, the following example outputs:

  None
  def

rather than:

  abc
  def

(Note that I've simplified this example a good bit so the behavior is
apparent.)

Randall

-------------------------------------------------------------------------------

import re

pattern = '(---(?P<id>[^-]*)---)|(===(?P<id>[^=]*)===)'

s = "---abc---"
match = re.compile( pattern ).match(s,0)
print match.group('id')

s = "===def==="
match = re.compile( pattern ).match(s,0)
print match.group('id')




More information about the Python-list mailing list