[Python-bugs-list] [ python-Bugs-529708 ] error in re docs or in sre

noreply@sourceforge.net noreply@sourceforge.net
Wed, 13 Mar 2002 22:50:59 -0800


Bugs item #529708, was opened at 2002-03-14 02:39
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=529708&group_id=5470

Category: Documentation
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Ben Wolfson (rumjuggler)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: error in re docs or in sre

Initial Comment:
The docs for positive lookbehind assertions state that
"(?<=abc)def will match "abcdef", since the lookbehind
will back up 3 characters and check if the contained
pattern matches", but that doesn't gibe with experience:

>>> import re
>>> f = re.compile('(?<=abc)def')
>>> print f.match('abcdef')
None
>>> 

I don't know enough about REs to know if this is a
documentation error or an sre error, though.

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

>Comment By: Fredrik Lundh (effbot)
Date: 2002-03-14 07:50

Message:
Logged In: YES 
user_id=38376

the documentation is confused.  <= means that the
pattern following the assertion ("def") will only
match if preceeded by the assertion ("abc").

>>> re.search("(?<=abc)def", "abcdef").group(0)
'def'

>>> re.search("(?<=abc)...", "abcdef").group(0)
'def'

>>> re.search("(?<=c)\w", "abcdef").group(0)
'd'


here's a slighty better example: match words, but
only if they're preceeded with a hyphen:

>>> re.search("(?<=-)\w+", "spam -egg").group(0)
'egg'


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

Comment By: Tim Peters (tim_one)
Date: 2002-03-14 07:42

Message:
Logged In: YES 
user_id=31435

Changed to Docs and reassigned to Fred.  Reassign to /F if 
it will help.

The example works fine (and matches 'def') if .search() is 
used instead of .match().  It shouldn't match if .match() 
is used (and doesn't).  This is a confusion due to the 
reader mistaking the word "match" used to describe that the 
regexp, well, *matches* <wink>, with the method named "match
()" that merely constrains the match to begin at the start 
of the string.

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

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=529708&group_id=5470