简体   繁体   English

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

[英]Replace with Regex in Notepad++

I'm trying to replace the part the text with the same text and some extra, example:我正在尝试用相同的文本和一些额外的内容替换文本部分,例如:

Initial text初始文本

<href="../doc/d5807346.pdf" class="document">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

After

<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>

I'm using in Notepad++ the following Regex:我在 Notepad++ 中使用以下正则表达式:

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

End result is not what I'm expecting, note the > between class="document" and download:最终结果不是我所期望的,请注意 class="document" 和下载之间的 >:

<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>

I'm stuck on trying to figure out how to prevent that.我一直在试图弄清楚如何防止这种情况发生。

Try this:试试这个:

  1. Find: (?:class="document">)([^<]*)查找:(?:class="document">)([^<]*)
  2. Replace (corrected): class="document">download="$1">$1替换(更正):class="document">download="$1">$1

Tested in N++在 N++ 中测试

Before: <href="../doc/d5807346.pdf" class="document">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>之前: <href="../doc/d5807346.pdf" class="document">3.2.1.&nbsp;&nbsp;&nbsp;&nbsp;EXAMPLE</a></div><div style="clear:both;"></div>

After: <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>之后: <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>

It's helpful to Use "online Regex tester" for visual debug regular expression使用“在线正则表达式测试器”进行可视化调试正则表达式很有帮助

  • Ctrl + H Ctrl + H
  • Find what: (class="document")>([^<]+)查找内容: (class="document")>([^<]+)
  • Replace with: $1 download="$2">$2替换为: $1 download="$2">$2
  • CHECK Wrap around CHECK环绕
  • CHECK Regular expression检查正则表达式
  • Replace all全部替换

Explanation:解释:

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

Replacement:替换:

$1              # content of group 1 + a space
download="      # literally
$2              # content of group 2
">              # literally
$2              # content f group 2

Screenshot (before):屏幕截图(之前):

在此处输入图像描述

Screenshot (after):截图(之后):

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM