Confused about 'positive lookbehind assertion'

Robert Dailey rcdailey at gmail.com
Tue Sep 25 10:45:06 EDT 2007


I think I get it... it's really just a way (so it seems) to make characters
get added to the found groups as they're matched. Thanks for your help.

On 9/25/07, Andrew Durdin <adurdin at gmail.com> wrote:
>
> On 9/25/07, Robert Dailey <rcdailey at gmail.com> wrote:
> > Hi,
> >
> > I've been reading the python documentation on 'positive lookbehind
> > assertion' and I don't understand at all how it works. The python docs
> give
> > the following example:
> >
> > " (?<=abc)def will find a match in "abcdef", since the lookbehind will
> back
> > up 3 characters and check if the contained pattern matches."
> >
> > Can anyone emphasize more on what this RE operation does? Thanks.
>
> It ensures that the regex will only match following the string in the
> lookbehind group, but without capturing that string.
>
> As the docs say, "(?<=abc)def" will match "abcdef"; but it will not
> match "def" (as the "abc" is not there).  If it does match,  the 0th
> group in the match object will be "def".
>
> In contrast, the regex "abcdef" will also match "abcdef" and not
> "def", but the 0th group will be "abcdef".
>
> The negative lookbehind is the opposite -- e.g. "(?<!abc)def" will
> match "def" but not "abcdef".
>
> Cheers,
>
> Andrew
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070925/b949b38f/attachment.html>


More information about the Python-list mailing list