Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex, select closest match

Tags:

python

regex

Assume the following word sequence

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

What I would like to do is to extract the text from BLA to LOOK, but the BLA which is the closest to look. I.e. I would like to get

BLA text text text text LOOK 

How should I do that using regular expressions? I got one solution which works, but which is exteremely inefficient.

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

Is there a better and more performant way to achieve matching this pattern?

What I would like to do is: I would like to match BLA, then forward lookahead until either positive fordward lookahead with LOOK or negative lookahead with BLA. But I don't know a way to put this into a regular expression.

As a engine I use re in python.

like image 447
overseas Avatar asked Jan 14 '15 08:01

overseas


1 Answers

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

Try this. See demo.

Alternatively, use

BLA(?:(?!BLA|LOOK)[\s\S])*LOOK

To be safer.

like image 71
vks Avatar answered Sep 17 '22 18:09

vks