Aleks
Aleks

Reputation: 35

Search for a block containing two words

I have some text

1 fgj sel dfjkd prm jfkdl frm jkl tbl klk

2 ledk prm jkl jkk frm jkl tbl jkl fjj

3 jklj sel prm kljlk jkjkl jkkl tbl emel

4 rui jkljl sel jklj jklj prm jkjl

5 jljkj frm jkljl jlj tbl mjljlk

I need the block that starts with "sel" and ends with "tbl". And this block contains two words "prm" and "frm". That is, the block consists of the sequence of words "sel" "prm" "frm" "tbl" And the correct answer should be (part of the first line, part of the fourth and fifth line)

sel dfjkd prm jfkdl frm jkl tbl

sel jklj jklj prm jkjl

5 jljkj frm jkljl jlj tbl

I tried, but it does not work for me

(sel)*?(prm|frm)?*(tbl)

or so

\bsel\b.*?\bprm\b.*?\bfrm\b.*?\btbl\b

Upvotes: 2

Views: 75

Answers (1)

revo
revo

Reputation: 48751

You need to use two positive lookaheads with a tempered ungreedy dot inside to put a boundary while looking for both prm and frm. This boundary is when reaching a tbl string. Try:

(?s)sel(?=((?!tbl).)*?prm)(?=(?1)*?frm)(?1)*tbl

See live demo here

Upvotes: 2

Related Questions