在 Notepad++ 中替换为正则表达式

Jua*_*njo 3 regex notepad++

我试图用相同的文本和一些额外的内容替换文本部分,例如:

初始文本

<href="../doc/d5807346.pdf" class="document">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>
Run Code Online (Sandbox Code Playgroud)

后

<href="../doc/d5807346.pdf" class="document" download="3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>
Run Code Online (Sandbox Code Playgroud)

我在 Notepad++ 中使用以下正则表达式:

  • 查找:((?<=class="document">).*?(?=<))
  • 替换为:download="\1">\1

最终结果不是我所期望的,请注意 class="document" 和 download 之间的 > :

<href="../doc/d5807346.pdf" class="document"> download="3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>
Run Code Online (Sandbox Code Playgroud)

我一直在试图找出如何防止这种情况发生。

Tot*_*oto 8

  • Ctrl+H
  • 找什么:(class="document")>([^<]+)
  • 用。。。来代替:$1 download="$2">$2
  • 检查 环绕
  • 检查 正则表达式
  • Replace all

解释:

(class="document")      # group 1
>                       # literally >
([^<]+)                 # group 2, 1 or more any character that is not "<"
Run Code Online (Sandbox Code Playgroud)

替代品:

$1              # content of group 1 + a space
download="      # literally
$2              # content of group 2
">              # literally
$2              # content f group 2
Run Code Online (Sandbox Code Playgroud)

截图(之前):

在此输入图像描述

截图(之后):

在此输入图像描述