I spent some time learning regular expressions, but I still don't understand how the next trick works with two words matching in different order .
import re reobj = re.compile(r'^(?=.*?(John))(?=.*?(Peter)).*$',re.MULTILINE) string = ''' John and Peter Peter and John James and Peter and John ''' re.findall(reobj,string)
result
[('John', 'Peter'), ('John', 'Peter'), ('John', 'Peter')]

( https://www.regex101.com/r/qW4rF4/1 )
I know that part (?=.* ) Positive Lookahead called Positive Lookahead , but how does it work in this situation?
Any explanation?
source share