Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text regex to find and replace whitespace between two xml or html tags?

I'm using Sublime Text and I need to come up with a regex that will find the whitespaces between a certain opening and closing tag and replace them with commas.

Example: Replace white space in

<tags>This is an example</tags>

so it becomes

<tags>This,is,an,example</tags>

Thanks!

like image 990
hankphone Avatar asked Sep 18 '14 21:09

hankphone


2 Answers

You have just to use a simple regex like:

\s+

And replace it with with a comma.

Working demo

enter image description here

like image 162
Federico Piazza Avatar answered Sep 22 '22 09:09

Federico Piazza


This will find instances of

<tags>...</tags> 

with whitespace between the tags

(<tags>\S+)\W(.+</tags>)

This will replace the first whitespace with a comma

\1,\2

Open Find and Replace [OS X Cmd+Opt+F :: Windows Ctrl+H]

Use the two values above to find and replace and use the 'Replace All' option. Repeat until all the whitespaces are converted to commas.

The best answer is probably a quick script but this will get you there fairly fast without needing to do any coding.

like image 25
jwpfox Avatar answered Sep 22 '22 09:09

jwpfox