Another Regexp Question

andrew cooke andrew at acooke.org
Mon Jul 5 20:10:46 EDT 2010


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? :o(

Thanks,
Andrew




More information about the Python-list mailing list