Another Regexp Question

A

andrew cooke

As ever, I guess it's most likely I've misunderstood something, but in
Python 2.6 lookback seems to actually be lookahead. All the following
tests pass:

from re import compile

assert compile('(a)b(?<=(?(2)x|c))(c)').match('abc')
assert not compile('(a)b(?<=(?(2)b|x))(c)').match('abc')
assert compile('(a)b(?<=(?(1)c|x))(c)').match('abc')

assert compile('(a)b(?=(?(2)x|c))(c)').match('abc')
assert not compile('(a)b(?=(?(2)b|x))(c)').match('abc')
assert compile('(a)b(?=(?(1)c|x))(c)').match('abc')

But it seems to me that the first block should fail, because they
check the match *before* the point in question.

Note that without group references these work as I would expected:

assert compile('(a)b(?<=b)(c)').match('abc')
assert not compile('(a)b(?<=c)(c)').match('abc')

assert not compile('(a)b(?=b)(c)').match('abc')
assert compile('(a)b(?=c)(c)').match('abc')

in which lookback does indeed lookback (note the asymmetry, while the
first examples were symmetrical).

What am I missing this time? :eek:(

Thanks,
Andrew
 
M

MRAB

andrew said:
As ever, I guess it's most likely I've misunderstood something, but in
Python 2.6 lookback seems to actually be lookahead. All the following
tests pass:

from re import compile

assert compile('(a)b(?<=(?(2)x|c))(c)').match('abc')
assert not compile('(a)b(?<=(?(2)b|x))(c)').match('abc')
assert compile('(a)b(?<=(?(1)c|x))(c)').match('abc')

assert compile('(a)b(?=(?(2)x|c))(c)').match('abc')
assert not compile('(a)b(?=(?(2)b|x))(c)').match('abc')
assert compile('(a)b(?=(?(1)c|x))(c)').match('abc')

But it seems to me that the first block should fail, because they
check the match *before* the point in question.
Both the first and third should fail, but they pass.
Note that without group references these work as I would expected:

assert compile('(a)b(?<=b)(c)').match('abc')
assert not compile('(a)b(?<=c)(c)').match('abc')

assert not compile('(a)b(?=b)(c)').match('abc')
assert compile('(a)b(?=c)(c)').match('abc')

in which lookback does indeed lookback (note the asymmetry, while the
first examples were symmetrical).

What am I missing this time? :eek:(
Nothing. It's a bug. :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top