The Autodidacts

Exploring the universe from the inside out

Regex is horrible, yet regex is amazing

And it’s not just for programmers

Note: this post is part of #100DaysToOffload, a challenge to publish 100 posts in 365 days. These posts are generally shorter and less polished than our normal posts; expect typos and unfiltered thoughts! View more posts in this series.

Programmers mostly know about Regex and dislike it. Normal people are mostly blissfully ignorant of the existence of RegEx.

To me, RegEx is like superpower that’s available to everyone, but most don’t bother to develop, because you have to eat lots of spinach.

What is RegEx (Regular Expressions)? It’s a language for describing patterns in text. If a human can describe a pattern (ie, the phone book has numbers with separators) then regex can usually describe that pattern!

You don’t need to be a programmer to use regex because it’s built into most text editors. Even LibreOffice and Word!

Here’s a gif that shows the power of regex:

GIF showing the power of regex (for non-programmers!) with a demo using the example of finding all telephone numbers and changing their format with LibreOffice Writer search and replace
A simple example: find all telephone numbers and change their format with LibreOffice Writer search and replace.

Regex’s power is even greater when combined with other tools (like multiple selections).

Regex is horrible because the syntax is hard to learn. But once you get it, the syntax is easy. Then, regex is horrible because the syntax isn’t the same for different regex “engines”.

There’s POSIX regex with basic (BRE) and extended (ERE) versions, and Perl/PCRE regex, and Vim regex (which I still haven’t figured out) with its \verymagic and \nomagic modes, and probably countless more. (I haven’t found any tool that converts between Regex variants. This seems silly.) Then there’s the fact that the regex engine used by various hip software like Helix and Ripgrep is a Rust crate that doesn’t support regex features that I consider table stakes: lookahead and lookbehind.

But if regex is worth using despite all that, it must be good! And it is.

Here’s my suggestion.

If you’re a programmer, write a tool that can translate between regex flavors.

If you’re a programmer who isn’t super familiar with regex, regexr.com is the best tool I’ve found for testing and debugging regex.

If you’re a normal person, next time you find yourself looking for a pattern – ie, you know some digits of a phone number, or want to find all jpegs without a date in the filename – that you can describe with words, or performing the same operation a bunch of times in a text file, ask a nearby programmer to help you write your first regex. Or email me.

Sign up for updates

Join the newsletter for curious and thoughtful people.
No Thanks

Great! Check your inbox and click the link to confirm your subscription.