A regular expression for matching a multi-line string starts with x, ends with y and contains z, but not x in the middle

Better explain with an example.

This is the text:

<li>hello 
THE WORDS
</li>

<li> cruel </li>

<li> world THE WORDS </li>

I want to find lines with <li>and ends with </li>and contains THE WORDS. I expect that will match only <li> hello THE WORDS </li>and <li> world THE WORDS </li>.

What I tried: (?s)<li>.*?(THE WORDS).*?</li>

In this case, the second coincidence <li> cruel </li> <li> world THE WORDS </li>.

I am using Sublime Text.

+4
source share
2 answers

EDIT: For new requirements, use this regex:

(?s)<li>(?:(?!</li>).)*?THE WORDS.*?</li>

Explanation

  • (?s)activates the mode DOTALL, allowing the point to match between lines
  • <li> matches letter characters
  • (?:(?!</li>).) , </li
  • *? ....
  • THE WORDS
    • *? ....
  • literal </li>

( ):

( Sublime):

x+[^xy]*z[^xy]*y+

. regex demo.

  • x+ x
  • [^xy]* , x, y
  • z z, ( )
  • [^xy]* , x, y
  • y+ y
+5

xx hello zz yy, xx world zz yy zz .

<li>(?:(?!<\/li>).)*(THE WORDS).*?<\/li>

DEMO

0

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


All Articles