The Sublime Regex

The Sublime Regex

Table of Contents

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.

alter-text

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.

alter-text alter-text

Enjoy!

Tags :

Related Posts

Just a Note Regarding Harry Potter

Just a Note Regarding Harry Potter

I just wanted to take a second and say that I nor anyone associated with this website approves of or appreciates the destructive and bigoted transphobic messages coming from J.K Rowling recently. If you are not familiar with the issues, ContraPoints does a fantastic job describing the issues.

While I say that, I do want to say, I really love Harry Potter and the wonderful world the books create. Such a delight and wonderful addition to the world.

Read More
The First Real Ride of Spring

The First Real Ride of Spring

Yesterday was the first real ride of spring. The weather was perfect, and the Todd and I headed north up the coast for a bit, then across the Mayport Ferry, before heading back to Sippers and having some coffee. It was a great ride. I love the winding road once you get past the Zoo and Cruise ship terminals, and Herschel heads out onto blunt island. The views are fantastic, and it stays interesting all the way up to Big Talbot island. Todd is breaking in his brand new shiny bike. It’s pretty nice.

Read More