Mental model of lookahead assertions

Python python at python.invalid
Thu Feb 27 17:39:11 EST 2020


Stefan Ram 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?
> 
>    (Something with nondeterminism?)

Old but EXCELLENT insight into how regular expressions works:

https://perl.plover.com/yak/regex/

(pdf and html slides)


More information about the Python-list mailing list