⌘ Harnessing the power of RegEx across multiple apps

RegEx is a powerful tool for finding and processing text. While it’s a full programming language, there are some basics that go a long way. Mastering just a few commands will save hours of time.

Almost every text editor on the Mac supports RegEx. My main use is with TextSoap, Keyboard Maestro, and the shell.

To start, I use TextSoap with RegEx to delete lines of text that match my criteria. In output files, I remove lines with INFO and Debug. In other files, I highlight Error or Warning.

The Keyboard Maestro search/replace modules support RegEx, so you can parse clipboard text or variables. As a simple example, I copy Jira ticket titles, remove line breaks and replace them with ‘ – ‘.

You don’t have to be an expert in RegEx to save yourself time and believe me, I’m no expert. However, there are plenty of tools and resources to help.

Stackoverflow is alive with RegEx questions and answers. People do some amazing things with genius answers that will make your jaw drop. Be prepared though, some solutions look like someone pounded the keyboard with ! @ ? < > all over the place.

You can go online to sites like https://regexr.com/ to practice and learn. Not to be outdone, the Mac has RegEx tools like Patterns and DevUtils. Load sample text and perfect your pattern.

If you want to go big, there are entire books dedicated to RegEx. Apress and O’Reilly have several.

You can still make big strides by starting small. For example, here are a couple of simple, but powerful starters.

  • \d{1,3} – Select numbers from 1 to 3 digits in length
  • \w{4} – Select words that have at least 4 letters in them
  • \s \t \n \r – Codes for space, tab, newline, return
  • \w{4}.* – Select the entire line if it has a word with at least 4 letters
  • (http|https):// – Find http or https

These aren’t complete solutions, but building blocks. I start with these combinations all the time. As I said, it’s a complex language all it’s own.

Since RegEx is integral to Unix, it’s used all over the place. It’s a key part of shell scripting especially when working with files. I use a Keyboard Maestro macro to move different kinds of files to folders based on the document title. It’s variations of (shell|unix) groupings.

I have this for multiple Keyboard Maestro macros and shell scripts.

Once you find one solution, it’s not too difficult to adapt and modify it to other uses. Investing a few hours in the right answer can save hours down the road. Take small steps and break the problem and resolution into small chunks. You’ll see lots of one-liners that are a little overwhelming.

People use RegEx all the time, which means all the things I’ve wanted to do already had 98% of an answer.

Like my other code, I have a library of RegEx solutions stored away in SnippetsLab. There is a lot of documentation on what’s going on, because it’s very easy to forget the nuances of RegEx. I also keep the same data it works against so I can drop it into Patterns and see exactly what it’s matching.

I was reintroduced to RegEx by a guy I used to work with 8 years ago. It was a simple and elegant solution and I’ve been intrigued and studying ever since. The fact it’s so prevalent with the Mac tools means you’ll eventually run in to it.

When your text parsing involves more than changing one word for another, RegEx has a whole bag of tricks to make the job easier.

Then again, I could be wrong.
Author Signature for Posts