The Sublime Regex

The Sublime Regex

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

Research Guide: Knitting History and Techniques

Research Guide: Knitting History and Techniques

For a recent class, I needed to create a beginner research guide for a topic that interests me.

Read More
Double Basket Beanie Pattern Is Live

Double Basket Beanie Pattern Is Live

I finally finished writing the pattern to my satisfaction and testing the various options.

Read More
My Favorite Everything Socks

My Favorite Everything Socks

My first package from the wonderful Woolens & Nosh yarn club was from October, 2023, and since it was my first, I wanted to make something special with it.

Read More