Confused about 'positive lookbehind assertion'

Karthik Gurusamy kar1107 at gmail.com
Tue Sep 25 22:55:52 CEST 2007


On Sep 25, 8:01 am, Erik Jones <e... at myemma.com> wrote:
> On Sep 24, 2007, at 9:38 PM, Robert Dailey 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.
>
> Have you actually tried it out?
>
>  >>> import re
>  >>> r = re.compile(r'(?<=abc)def')
>  >>> m1 = r.search('aaaabcdeffff')
>  >>> m1.group()'def'
> 'def'
>  >>> m2 = r.search('bcdefff')
>  >>> m2 == None
> True
>
> So, it matches 'def' but only if it is immediately preceded by 'abc'.

Any idea what this positive lookbehind achieves which can't be done
without it.
I remember cases where positive look-ahead is useful.

In the above example, r.search('abcdef') does the job of ensuring
'def' is preceded by 'abc'.

Karthik

>
> Erik Jones
>
> Software Developer | Emma®
> e... at myemma.com
> 800.595.4401 or 615.292.5888
> 615.292.0777 (fax)
>
> Emma helps organizations everywhere communicate & market in style.
> Visit us online athttp://www.myemma.com





More information about the Python-list mailing list