In the last few days I had to use stackoverflow in which I found a great and helpful developers. Thanks to all!!
I am working on big project, and I need a help a little bit. I work with moodle xml file on Sublime Text 3.
This is small part of the code I have:
<questiontext format="html">
<text>Here is my sample text</text>
</questiontext>
I need to transform it to this:
<questiontext format="html">
<text><![CDATA[<audio preload="auto"><source src="https://www.mywebsite.com/sample.mp3"></audio>Here is my sample text?]]></text>
</questiontext>
The thing is that I have about 400 lines which I need to change this way. And the text "Here is my sample text" is different everywhere. How can I make such transformation? Create a snippet? To be honest, I have looked at how to create snippet, but can't figure out how to create such one like this... All sugestions are welcome
A snippet is used for entering often-used text (i.e. a snippet of text) to save you time when entering new text. If you already have text that you need modified, a snippet is not the answer.
For that, you want to do something with find and replace, and in particular using a Regular Expression to find and match the text that you want.
For your sample above, the process would be something like the following:
Open the replace panel by selecting Find > Replace...
from the menu or using the appropriate key for your operating system (it's visible in the menu if you don't know it).
In the panel that appears that the bottom, make sure that "regular expressions" and "Highlight matches" are turned on, and "Preserve Case" is turned off. See the tool tips on the buttons.
Next, in the Find What
, enter the text you want to find, which for your example would be:
<text>(.*)</text>
The regular expression part of this is (.*)
which is saying "match any sequence of characters, including none at all, and capture (save) what you have selected".
As you enter the text, if you watch in the buffer you will see Sublime outlining the text that is being matched, since we turned on "highlight matches".
In the Replace With
panel, enter the following text:
<text><![CDATA[<audio preload="auto"><source src="https://www.mywebsite.com/sample.mp3"></audio>\1\?]]</text>
The magic part here is that bit near the end that says \1\?
; that says "insert the first thing we captured in the find field, followed by a question mark"; the rest of the text is what you specified in your question.
If your example doesn't really need to have a question mark at the end of the text (in your sample you have added it), you don't need the \?
part.
Now you can press Replace
or Replace All
, and all of the instances of <text>something</text>
will be replaced with the replacement string, except that it doesn't matter what the something
part is because the text from the original line will be retained.
Depending on your actual circumstances you may need to tweak this as appropriate (say if your example text isn't laid out very nicely or something).
I recommend something like this tutorial (which I also linked to above) to get more in depth understanding of what's going on. In particular, that will explain why, in order to add a question mark, you need to use \?
and not just ?
, which I totally glossed over because it's a great exercise for the reader. ;)