正则表达式在字符串中查找单词,替换新字符串中的单词(使用 Notepad++) [英] regex find word in string, replace word in new string (using Notepad++)

查看:59
本文介绍了正则表达式在字符串中查找单词,替换新字符串中的单词(使用 Notepad++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前发布了这个问题的简化版本,但我想我可能把它简化了太多,所以这里是实际问题.

I posted a simplified version of this question before, but I think I might have simplified it too much, so here is the actual problem.

我想使用正则表达式(在 Notepad++ 或类似版本中)在以下内容中找到a_dog"(对不起墙):

I want to use regex (in Notepad++ or similar) to find "a_dog" in the following (sorry about the wall):

<object classid="clsid:D27CDB6E-AE6D-11cf" id="FlashID">
<param name="movie" value="../flash/words/a_dog.swf">
<param name="quality" value="high">
<param name="wmode" value="opaque">
<param name="swfversion" value="6.0.65.0">
<!--[if !IE]>-->
  <object data="../flash/words/a_dog.swf" type="application/x-shockwave-flash">
     <!--<![endif]-->
     <param name="quality" value="high">
     <param name="wmode" value="opaque">
     <param name="swfversion" value="6.0.65.0">
     <!--[if !IE]>-->
  </object>
<!--<![endif]-->
</object>

然后我想使用后向引用将 øø 的所有实例替换为 a_dog 如下:

Then I want to use a back-reference to replace all instances of øø with a_dog in the following:

<input type="button" class="ButtonNormal" onClick="audio_func_øø()">
 <script>
    function audio_func_øø() {
    var playAudio = document.getElementById("element_øø");
    playAudio.play();
    }
 </script>
 <audio id="element_øø">
    <source src="../audio/words/øø.mp3" type='audio/mpeg'>
    <source src="../audio/words/øø.wav" type='audio/wav'>
 </audio>

这样就只剩下第二个代码了(用 a_dog 而不是 øø),第一个代码的痕迹就没有了.

So that only the second code is left (with a_dog instead of øø), and no trace of the first code remains.

推荐答案

我不知道如何在 Notepad++ 中执行此操作,但您可以在 SublimeText 使用正则表达式、片段和多项选择:

I don't know how to do this in Notepad++, but you can do this in SublimeText using regex, snippets, and multiple selection:

首先制作一个新片段(指南) 包含以下内容:

First make a new snippet (guide) with the following in it:

<snippet>
    <content><![CDATA[

<input type="button" class="ButtonNormal" onClick="audio_func_$1()">
<script>
    function audio_func_$2() {
        var playAudio = document.getElementById("element_$3");
        playAudio.play();
    }
</script>
<audio id="element_$4">
    <source src="../audio/words/$5.mp3" type='audio/mpeg'>
    <source src="../audio/words/$6.wav" type='audio/wav'>
</audio>

    ]]></content>

    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>audioSnippet</tabTrigger>
</snippet>

将其另存为您喜欢的用户包中的任何内容.如果您对如何/在哪里保存它以使其正常工作有任何疑问,请按照链接的文章进行操作.稍后我将讨论它是如何工作的.

Save it as whatever you like in your User package. Follow the linked article if you have any questions on how/where to save it to get it working. I will discuss how this works later on.

接下来通过使用以下模式搜索(启用正则表达式)在 Sublime Text 中使用以下正则表达式:

Next use the following regex in Sublime Text by searching (with regex enabled) using the following pattern:

(?<=value="../flash/words/).+(?=\.swf)

然后点击查找全部" - 这将使用 多选.

And hit "Find All" - this will select all the names (e.g. 'a_dog', 'a_cat', 'a_plane') using multiple selection.