Negative Lookahead Problem

Roger Day roger.day at globalgraphics.com
Thu Dec 5 07:11:54 EST 2002


The following code btn the dashes:
----------------------------------------
import re

path = ["/users/dibbl","users/rsrc"]
print "expect 1st element to be selected"
m = re.compile(".*(?!rsrc$)")
for p in path:
    kk = m.match( p )
    if kk:
        print p
print "expect 2nd element to be selected"
m = re.compile(".*(?=rsrc$)")
for p in path:
    kk = m.match( p )
    if kk:
        print p
---------------------------------------
produces this output:

expect 1st element to be selected
/users/dibbl
users/rsrc
expect 2nd element to be selected
users/rsrc

In the first test case, the negative-lookahead "fails"
 and the pattern selects both elements. It looks like 
my understanding of negative-lookahead is woefully 
short of the mark. Can someone please explain the 
behaviour of negative lookahead in this case? Is 
there another way of selecting, by regular expression, 
a line which doesn't end in a certain set of characters?

TIA

Roger.



More information about the Python-list mailing list