Sublime Forum

RegEx help needed

#1

A bit OT, but I need to do this in ST2:

Someone gave me an XML file where one of the child nodes for each entry is . They gave me image names with spaces. I’d like to find all the spaces between words inside the node and replace with an underscore.

for example, Tomato_Hartmans Yellow Gooseberry.jpg needs to be Tomato_Hartmans_Yellow_Gooseberry.jpg

Is this even possible in ST2?

0 Likes

#2

Still like to know if possible with ST2, but I did it with SED:

sed '/image/s/\ /_/g' ~/Desktop/tomatoes.xml > ~/Desktop/tomatoes2.xml
0 Likes

#3

Yes, you might be able to just use python from the console or you can create a plugin to do it using the re module.

0 Likes

#4

The RegReplace plugin would allow you to do this.

Just setup a replacement definition to find an empty space in a image file name inside an image tag and setup the replacement to replace the space with an underline. I added a feature for when it is hard to define a regex to find an unknown number of targets; you can create a command that will sweep the file over and over until all instances are found (or until you hit the sweep threshold, always have to look out for infinite loops). If you are interested in trying the plugin, but are having troubles with it, let me know and I can help.

0 Likes

#5

Here is a link to @facelessuser’s plugin github.com/facelessuser/RegReplace
or use Package Control as per usual wbond.net/sublime_packages/package_control

0 Likes