Home Download Buy Blog Forum Support

RegEx help needed

RegEx help needed

Postby handycam on Fri Jan 27, 2012 1:41 pm

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 <image>. They gave me image names with spaces. I'd like to find all the spaces between words inside the <image> node and replace with an underscore.

for example, <image>Tomato_Hartmans Yellow Gooseberry.jpg</image> needs to be <image>Tomato_Hartmans_Yellow_Gooseberry.jpg</image>

Is this even possible in ST2?
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

Re: RegEx help needed

Postby handycam on Fri Jan 27, 2012 1:51 pm

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

Code: Select all
sed '/image/s/\ /_/g' ~/Desktop/tomatoes.xml > ~/Desktop/tomatoes2.xml
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

Re: RegEx help needed

Postby atomi on Fri Jan 27, 2012 3:48 pm

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.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: RegEx help needed

Postby facelessuser on Fri Jan 27, 2012 4:46 pm

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.
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: RegEx help needed

Postby atomi on Fri Jan 27, 2012 7:13 pm

Here is a link to @facelessuser's plugin https://github.com/facelessuser/RegReplace
or use Package Control as per usual http://wbond.net/sublime_packages/package_control
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US


Return to General Discussion

Who is online

Users browsing this forum: ChogyDan, Google [Bot] and 29 guests