正则表达式:选择所有不以特定标签结尾的行

正则表达式:选择所有不以特定标签结尾的行

我有这四行,除了最后两行之外,其余均以标签开头<p class="TEST">并以标签结尾。<br>

<p class="TEST">My mother is at home.<br>
<p class="TEST">My father is at home.<br>
<p class="TEXT">My sister is at home.<LLbr>
<p class="TEXT">My brother is at home.<AAbr>

因此,我想找到所有带有 TEXT 标签且不以<br>

我的输出结果应该是

<p class="TEXT">My sister is at home.<LLbr>
<p class="TEXT">My brother is at home.<AAbr>

我制作了一个正则表达式,但不太好。 (?-s)(.*<p class="TEXT">.*)(?-s)(?!)<br>(.*)$

答案1

  • Ctrl+H
  • 找什么:<p class="TEXT">(?:(?!<br>)(?!<p).)*(?:<.+?>|\z)
  • 取消勾选匹配大小写
  • 检查环绕
  • 检查正则表达式
  • 取消选中. matches newline
  • Search in document

解释:

<p class="TEXT">    # literally
(?:                 # start non capture group
  (?!<br>)          # negative lookahead, make sure we haven't <br>
  (?!<p)            # negative lookahead, make sure we haven't <p
  .                 # any character but newline
)*                  # group may appear 0 or more times
(?:                 # non capture group
   <.+?>            # a tag
 |                  # OR
   \z               # end of string
)                   # end of group

在此处输入图片描述

演示

相关内容