In Sublime Text, Gedit or any Other editor, how to select and replace everything inside Tags?

StackOverflow https://stackoverflow.com/questions/17850142

Вопрос

I have a database backup of a wordpress file. I need to replace all the captions with a div. I need to replace the whole caption tag [caption id="dfdsf"] and the closing tag [/caption] (with the braces) with a div. How can I do this? Replacing the closing tag will be easy. But searching for the opening tag is difficult because there could be anything inside it! How to do this?

Here's an example :

[caption id="attachment_34552" align="aligncenter" width="550"]<img class="size-large wp-image-34552" alt="Octopress" src="http://....jpg" width="550" height="385" /> Octopress [/caption]
Это было полезно?

Решение

If you don't mind regexes, the regex that matches the opening is

/\[caption[^]]*\]/

http://rubular.com/r/eRRxYIsVCG

remember to specify case insensitivity if you're going to have caption and CAPTION, etc.

if you just want to do this on the command line you can use sed(1)

sed 's/\[caption[^]]*\]/<div class=\\"sss\\">/g' file.html

which sends it to stdin, which you can pipe to whatever file you want.

if you want to modify the file in-place you can add the -i switch to sed.

Другие советы

Try the expand selection command. It's ctrl+shift+m in Windows and Linux. I can't recall the OS X bindings off the top of my head. You can see the binding for it by selection the Selection menu if you are unsure (assuming you haven't overridden it yet). Place the cursor somewhere within the square braces. The first expansion will not include the square braces, but the second will.

You can use sed from the terminal in a unix machine.

The command would be like

sed "s/\[caption \([^]]*\)\]/<div \1>/g" youdata.txt > file.txt 
sed "s/\[/caption\]/</div>/g" file.txt > file.txt
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top