Match two words in random order with a regular expression

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')] 

enter image description here

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

+6
source share
1 answer

It just does not correspond to an arbitrary order. Capture here is done with .* , Which consumes everything that comes to his mind. positive lookahead makes a statement. You have two lookaheads . They are independent of each other.Each makes a statement in one word. So your regex works like:

1) (?=.*?(John)) === The string must have John . Just a statement. Consumes nothing

2) (?=.*?(Peter)) === The string must have Peter . Just a statement. Consumes nothing

3) .* === Consume anything if statements have passed

So you see that the order here does not matter. What imp is is assertions should pass .

+1
source

Source: https://habr.com/ru/post/984735/


All Articles