six colors

by Jason Snell & Dan Moren

This week's sponsor

Photos: A Take Control Crash Course - Jason's ebook about Photos, now updated for High Sierra & iOS 11.

By Jason Snell

The joy of regex

This week the Internet’s Dr. Drang wrote a song of praise to Patterns, a clever $3 utility that I reviewed for Macworld early last year.

Patterns is a regular-expression utility, and Dr. Drang’s post is as good as an excuse as any for me to once again sing the praises of regular expressions, or regex, also known as pattern matching.

Put simply, I have saved hundreds of hours of drudgery in the years since I read Mastering Regular Expressions by Jeffrey E.F. Friedl and learned how to use regular expressions to search and replace text.

As Dr. Drang put it:

If you’re not familiar with regular expressions, you should be, especially if you’re interested in automating your work with a computer. They are a sort of mini-language for describing patterns of text, and knowing them will greatly expand your ability to search and replace. I first learned regex syntax back in the mid-90s from the “Searching with Grep” chapter in the BBEdit manual (a chapter written by John Gruber, if I remember correctly). If you don’t own BBEdit, there are millions of regex tutorials on the web. I suggest David Mertz’s short introduction. I have his Text Processing in Python book, and he’s a clear and succinct writer. If you really get into regexes, you can move on to Jeffrey Freidl’s book.

I couldn’t agree more. I had already learned a lot about regular expressions before I encountered that chapter in the BBEdit manual, but I still use it (as well as a PDF version of Friedl’s book) as references on occasion. I use regular expressions to fix HTML, to reformat text pasted in from a spreadsheet, to rename and rewrite code in my website content-management system.

Where Patterns comes in is that it’s a useful tool to let you test your regular expression before doing the searching and replacing. This is more important if you’re coding than if you’re just transforming text in BBEdit, but it’s also a great tool for learning what works and what doesn’t work. Like Dr. Drang, I wish I had access to an interactive tool like Patterns when I was learning regular expressions ages ago.

[If you appreciate articles like this one, help us continue doing Six Colors (and get some fun benefits) by becoming a Six Colors subscriber.]