【问题标题】:Multiple find and replace in notepad++记事本++中的多个查找和替换
【发布时间】:2014-07-18 04:39:11
【问题描述】:

我在 Notepad++ 中使用正则表达式来查找 <span class="bold">(.*?)</span> 并将其替换为 <strong>\1</strong> 以及将 <span class="italic">(.*?)</span> 替换为 <i>\1</i>。我必须对很多文档执行此操作,并且想知道我是否可以使用单个查找和替换来完成这两个操作。

【问题讨论】:

  • 不,至少不现实..但是您应该能够在所有文档中进行查找/替换,然后只需执行两次。
  • 也许notepad++ 是一个错误的工具。
  • 我知道 notepad++ 确实“在文件中查找”,它是否允许“在文件中查找和替换”?否则,是的,可能是该工作的错误工具。
  • 对不起,我应该说“页面”而不是“文档”。我在文件中找到了查找和替换,但没有很好的文件集合。我会用我所拥有的。谢谢。

标签: regex notepad++


【解决方案1】:

您可以考虑使用sed 使用单个命令行来完成此任务。下面的示例将在给定目录中的所有 .txt 文件中查找/替换多个模式/替换。

sed -e 's/pattern1/replacement1/g;s/pattern2/replacement2/g' *.txt

要实际替换这些模式,请使用i 选项。 -r 选项允许扩展正则表达式。

sed -i -re 's!<span class="bold">(.*?)</span>!<strong>\1</strong>!g;s!<span class="italic">(.*?)</span>!<i>\1</i>!g' *.txt

【讨论】:

    【解决方案2】:

    我想出了一些技巧来完成这项工作,但是它只能使 &lt;span class="bold"&gt; 变为 &lt;b&gt; 而不是 &lt;strong&gt;,因为它从类中捕获了一个字符:

    <span class="(b(?=old)|i(?=talic))[^"]+">(.*?)<\/span>
    <\1>\2</\1>
    

    Demo


    说明:

    <span class="
    (             (?# start capture group for new element)
      b           (?# match b...)
      (?=old)     (?# followed by old)
     |            (?# OR)
      i           (?# match i)
      (?=talic)   (?# followed by italic)
    )             (?# end capture group)
    [^"]+         (?# match non-" characters that were found in lookaheads)
    ">
    (.*?)         (?# lazily capture the contents of the span)
    <\/span>
    

    但是你应该可以用 Notepad++ 来find/replace in all files...

    【讨论】:

      猜你喜欢
      • 2017-08-09
      • 2013-11-23
      • 2013-08-20
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 2019-02-10
      • 2016-10-07
      相关资源
      最近更新 更多