Moogal
Moogal

Reputation: 107

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.

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>

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>

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

Upvotes: 3

Views: 867

Answers (1)

Jay
Jay

Reputation: 19897

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.

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.

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

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.

I've made a screen capture that you can look at here: http://youtu.be/oo2MQV3X244 (unlisted video on YouTube)

Upvotes: 4

Related Questions