Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

python - Regex, select closest match

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
(?s)BLA(?:(?!BLA).)*?LOOK

Try this. See demo.

Alternatively, use

BLA(?:(?!BLA|LOOK)[sS])*LOOK

To be safer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...