The Sublime Regex

Posted on Fri 25 April 2014 in Technology

Sublime Text is a really wonderful text editor. It's flexible and extensible enough to cover just about any need, but still simple and clean to use. This post is not a long discussion about either text editors or regular expressions, but a simple post about a problem I needed to tackle and the simple solution.

I pulled a bunch of data from our issue tracking software, and I needed to create a simple list of all the issues resolved and the person assigned the issue. Simple. I grabbed the text I needed from the csv file created, then used a simple find/replace in Sublime Text to put the text in the format I ultimately wanted.

The change is simple. I have a long list of issue summaries followed by a comma and the persons name. All I want to do is find the last comma in each line and replace the comma with a colon and a space.

The sample starts like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor

The regex is fairly straight forward. Think of it this way, "I want to find the comma that isn't followed by another comma." That will guide us to a negative lookahead instead of trying to decipher "the last comma."

,(?!.*,)

Regular Expressions can be very confusing, and incredibly frustrating. Patience, and plenty of google juice are required. In Sublime Text, open the Find / Replace panel, and select the Regular Expression option.

Our regular expression basically says, "find any comma that is not followed by another comma." Sublime Text, if the option is enabled, shows which items match our find criteria, so it is just a matter of adding the Replace with text and clicking Replace All.

sublimeregex2

Enjoy!