Hello. There is an arbitrary 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 

It is necessary to take regular blocks that start with sel and end with tbl and at the same time contain prm and frm. Those. The block consists of a sequence of words sel prm frm tbl. As a result, the example should take part of the first line and 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 

He tried, but did not go.

 (sel)*?(prm|frm)?*(tbl) 
  • And the fifth line is why? - Andrey NOP
  • First, try the \bsel\b.*\b(prm|frm)\b.*\btbl\b expression \bsel\b.*\b(prm|frm)\b.*\btbl\b , here \b is the word boundary, .* any number of any characters - Andrew NOP
  • the fifth because on the fourth beginning of the block on the fifth end of the block, the text is multiline, on the fourth sel the beginning of the block, and on the fifth end of the tbl block - Aleks
  • \ bsel \ b. * \ b (prm | frm) \ b. * \ btbl \ b does not work correctly. The first line is captured correctly. The third is not true, because there is no word frm in it. part of the fourth and fifth lines are not captured (( - Aleks
  • Now everything is clear, see the answer - Andrew NOP

1 answer 1

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

And check "and new lines"

All 4 words you need are listed here, each word is framed with special quantifiers \b that mark the word boundary and are themselves zero-length, i.e. the expression \bsel\b captures the word sel , but will not capture select or useless .

Also, words are separated by the expression .*? here . - any character, * - any number ? - makes this expression lazy, so this expression captures any sequence of characters, but its length will be minimal enough, if this expression is not made lazy, then the full regular schedule will capture only one block - from the first sel to the last tbl .