Confused about 'positive lookbehind assertion'

Erik Jones erik at myemma.com
Tue Sep 25 11:01:45 EDT 2007


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'.

Erik Jones

Software Developer | Emma®
erik 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 at http://www.myemma.com





More information about the Python-list mailing list