[Python-bugs-list] [ python-Bugs-529708 ] error in re docs or in sre
noreply@sourceforge.net
noreply@sourceforge.net
Fri, 15 Mar 2002 22:26:41 -0800
Bugs item #529708, was opened at 2002-03-13 20: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: Closed
>Resolution: Fixed
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: Fred L. Drake, Jr. (fdrake)
Date: 2002-03-16 01:26
Message:
Logged In: YES
user_id=3066
Added clarifications and examples in Doc/lib/libre.tex
revisions 1.78, 1.73.6.3, and 1.60.2.4.
----------------------------------------------------------------------
Comment By: Fredrik Lundh (effbot)
Date: 2002-03-14 01: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 01: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