简体   繁体   中英

Double look-ahead assertion in python

I know that look ahead assertions matches based on the condition. But suddenly I was struck by this double look-ahead assertion.

>>> a = compile(r'a(?=b)(?=c)')
>>> b = a.findall('abc')
>>> b
[]

Then what it matches. Thanks in advance!

You are matching a and assert that after a, there should be a b . That assertions succeeds.

But after that, you assert that what comes after the a should be a c . That assertion fails, so there will be no match.

This for example a(?=b) will succeed and matches a .

This a(?=c) will not succeed because there is no c after a.

To assert that there is bc after a you might use a(?=bc) or an assertion inside an assertion a(?=b(?=c)) as @Keyur Potdar points out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM