Another Regexp Question
MRAB
python at mrabarnett.plus.com
Mon Jul 5 20:56:40 EDT 2010
andrew cooke wrote:
> 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? :o(
>
Nothing. It's a bug. :-(
More information about the Python-list
mailing list