正则表达式:将一些标签放在行首,但仅限于注释 html 中框出的标签

正则表达式:将一些标签放在行首,但仅限于注释 html 中框出的标签

我想在行首放置一些标签,但仅限于注释 html 中框出的标签。例如:

<!-- ARTICOL START -->

<p class="text_obisnuit">I want go home</p>         <p class="text_obisnuit2">Tv is my passion <em>"It is true"</em></p>         <p class="text_obisnuit">No one is here</p>  <p class="text_obisnuit2">God is my hero</p>

<!-- ARTICOL FINAL -->

输出应为:

<!-- ARTICOL START -->

<p class="text_obisnuit">I want go home</p>
<p class="text_obisnuit2">Tv is my passion <em>"It is true"</em></p>
<p class="text_obisnuit">No one is here</p>
<p class="text_obisnuit2">God is my hero</p>

<!-- ARTICOL FINAL -->

我的正则表达式不好:

寻找:

(<\!-- ARTICOL START -->)(.*)(<p class="text_obisnuit2">.*?</p>)|(<p class="text_obisnuit">.*?</p>)(.*)(<\!-- ARTICOL FINAL -->)

替换为: \1\2\r\3\4\5

或者

(<\!-- ARTICOL START -->)(.*)(<p class=".*?">.*?</p>)(.*)(<\!-- ARTICOL FINAL -->)

替换为: \1\2\r\3\4\5

答案1

  • Ctrl+H
  • 找什么:(?:.*?<!-- ARTICOL START -->|\G).*?</p>\K\h+(?=<p .*?<!-- ARTICOL FINAL -->)
  • 替换为:\n 或,\r\n具体取决于您的平台。
  • 查看 环绕
  • 查看 正则表达式
  • 查看 . matches newline
  • Replace all

解释:

(?:                         # non capture group
    .*?                         # 0 or more any character, not greedy
    <!-- ARTICOL START -->      # literally
  |                           # OR
    \G                          # restart from last match position
)                           # end group
.*?                         # 0 or more any character, not greedy
</p>                        # closing tag
\K                          # forget all we have seen until this position
\h+                         # 1 or more horizontal spaces
(?=                         # positive lookahead, make sure we have after:
    <p                          # openning tag, don't forget the space just after "<p"
    .*?                         # 0 or more any character, not greedy
    <!-- ARTICOL FINAL -->      # literally
)                           # end lookahead

截图(之前):

在此处输入图片描述

截图(之后):

在此处输入图片描述

相关内容