Regex, select the closest match

Suppose the following sequence of words

BLA text text text text text text BLA text text text text LOOK text text text BLA text text BLA 

I would like to do to extract the text from the BLA in the LOOK, but the BLA that is closest to viewing. That is, I would like to receive

 BLA text text text text LOOK 

How can I do this with regular expressions? I have one solution that works, but which is extremely inefficient.

 BLA(?!.*?BLA.*?LOOK).*?LOOK 

Is there a better and more efficient way to match this pattern?

What I would like to do is: I would match the BLA and then go into lookahead until a positive reverse expression with LOOK or a negative look with BLA appears. But I do not know how to make this a regular expression.

As an engine, I use re in python.

+6
source share
2 answers
 BLA(?:(?!BLA).)*?LOOK 

Try it. Check out the demo.

https://regex101.com/r/fA6wE2/12

or

 BLA(?:(?!BLA|LOOK).)*?LOOK 

To be more secure.

+7
source

just find the text between LOOK and BLA without BLA

 In : re.search(r'BLA [^(BLA)]+ LOOK', 'BLA text text text text text text BLA text text text text LOOK text text text BLA text text BLA').group() Out: 'BLA text text text text LOOK' 

:-)

0
source

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


All Articles