正则表达式在字符串中查找单词,替换新字符串中的单词(使用 Notepad++) [英] regex find word in string, replace word in new string (using 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.
- 复制选定的单词(
Ctrl+C
或系统上的等效项) - 在菜单中,选择->扩展到段落(这将选择
开始的位置,
</object>
结束的位置)立> - 按 Delete/Backspace 删除
的
- 输入您的代码段快捷方式(上面我已将其定义为audioSnippet")并点击 Tab
- 粘贴复制的文本(
Ctrl+V
或系统上的等效文本)
- Copy the selected words (
Ctrl+C
or equivalent on your system) - In the menu, Selection->Expand to Paragraph (This will select where the
<object>
begins, to where</object>
ends) - Hit Delete/Backspace to remove the
<object>
's - Type in your snippet shortcut (above I've defined it to be "audioSnippet") and hit Tab
- Paste in your copied text (
Ctrl+V
or equivalent on your system)
您会注意到您只替换了出现 $1
的代码段中的文本.您需要按 Tab 键跳转到 $2
,再次粘贴文本 (Ctrl+V
),然后重复直到到达制表位 $6
>.
You will notice that you have only replaced the text in the snippet where the $1
appears. you will need to hit Tab to jump to $2
, paste the text again (Ctrl+V
), and repeat until you get to tab stop $6
.
我制作了一个屏幕截图,您可以在此处查看:http://youtu.be/oo2MQV3X244(YouTube 上未列出的视频)
I've made a screen capture that you can look at here: http://youtu.be/oo2MQV3X244 (unlisted video on YouTube)
这篇关于正则表达式在字符串中查找单词,替换新字符串中的单词(使用 Notepad++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!