首页 > 解决方案 > Notepad ++ 中的复杂查找和替换

问题描述

我有数百个包含以下内容的文件,我想摆脱整个块。

   <texttool id="468" rect="55,306,319,23">
      <toolstroke />
      <toolcolor />
      <font style="2" size="18" />
      <Text>(Be sure to state the page/problem number.)</Text>
    </texttool>

问题是它们都有不同的id=XXX部分......不过其他一切都是一样的。

有没有办法进行大规模查找和替换来处理这种情况?

标签: notepad++

解决方案


使用以下正则表达式将搜索整个文件并删除所有 <texttool>块以及其中的内容:

(<texttool(?:.|\n)*?<\/texttool>)

 text before<texttool id="468" rect="55,306,319,23">
      <toolstroke />
      <toolcolor />
      <font style="2" size="18" />
      <Text>(Be sure to state the page/problem number.)</Text>
    </texttool> text after

<texttool id="468" rect="55,306,319,23">
      <toolstroke />
      <toolcolor />
      <font style="2" size="18" />
      <Text>(Be sure to state the page/problem number.)</Text>
    </texttool>

 text before text after

您可以在此DEMO中亲自尝试

更新 1

根据要求,以下正则表达式将仅删除<texttool>包含以下属性的那些 - rect="55,306,319,23"

(<texttool.*rect=\"55\,306\,319\,23\"(?:.|\n)*?<\/texttool>)

这是更新的 Regex DEMO

请注意,它只会匹配那些包含该特定字符串并匹配其文字字符的块。

更新 2

我提供的正则表达式在 Notepad++ 中无法正常工作,因为它使用了基于自定义 PCRE 的正则表达式系统。这是一个对我有用的经过测试和验证的模式:

<\btexttool.*\brect\=\"55\,306\,319\,23\"([\s\S]*?)<\/\btexttool>

禁用Notepad++ 搜索窗口中的选项非常重要. matches newline,否则该模式将不起作用,因为提供的模式与它不兼容。


推荐阅读