Mental model of lookahead assertions

Jon Ribbens jon+usenet at unequivocal.eu
Fri Feb 28 06:02:32 EST 2020


On 2020-02-27, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>   One can count overlapping occurences as follows.
>
>|>>> print(len(findall('(?=aa)','caaaab')))
>|3
>
>   Every web page says that lookahead assertions do
>   not consume nor move the "current position".
>
>   But what mental model can I make of the regex
>   engine that explains why it is not caught in an
>   endless loop matching "aa" at the same position
>   again and again and never advancing to the other
>   occurences?

Simply that when it's returning multiple matches it knows once it's
found a match at position <x> to start looking for the next potential
match at position <x + 1> ?


More information about the Python-list mailing list