[Python-bugs-list] [ python-Bugs-814253 ] Grouprefs in lookbehind assertions

SourceForge.net noreply at sourceforge.net
Sun Nov 2 10:31:13 EST 2003

Bugs item #814253, was opened at 2003-09-28 19:31
Message generated for change (Comment added) made by glchapman
You can respond by visiting: 

Category: Regular Expressions
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Greg Chapman (glchapman)
Assigned to: Fredrik Lundh (effbot)
Summary: Grouprefs in lookbehind assertions

Initial Comment:
I was trying to get a pattern like this to work:

   pat = re.compile(r'(?<=(...)\1)abc')
   pat.match('jkljklabc', 6)

Unfortunately, that doesn't work.  The problem is that 
sre_parse.Subpattern.getwidth() ignores GROUPREFs 
when calculating the width, so the subpattern in the 
assertion is deemed to have length of 3 (I was hoping 
that sre could detect that the group 1 had a fixed 
length, so the reference to it would also have a fixed 

I've since discovered that both Perl and PerlRE cannot 
handle the above pattern, but they both generate 
exceptions indicating that the assertion has a variable 
length pattern.  I think it would be a good idea if sre 
generated an exception as well (rather than silently 
ignoring GROUPREFs).


>Comment By: Greg Chapman (glchapman)
Date: 2003-11-02 06:31

Logged In: YES 

Attached is a patch which gives GROUPREFs an arbitrary 
variable width, so that they raise an exception if used in a 
lookbehind assertion.  Obviously, it would be better if 
GROUPREFs returned the length of the group to which they 
refer, but I don't see any obvious way for getwidth() to get 
that information (perhaps I missed something?).


You can respond by visiting: 

More information about the Python-bugs-list mailing list