Computers are built to make repetitive tasks easier. And yet, all too often human operators spend hours on tasks that could easily be automated. If you have to manage a ton of data or files, you can improve your productivity with regular expressions.
This guide will already assume you’re familiar with the basics of regular expressions. If you want to brush up on the basics, check out this regular expressions cheatsheet and this regular expressions tutorial. Here are some daily tasks where regular expressions can improve your productivity.
1. Rename files with complex rules
Any savvy computer user can handle renaming a pile of files with a simple text string and incrementing counter. It’s harder, however, to rename files following a certain protocol. Let’s say you want to rename files based on their current file names, current directories, modified dates or some other attributes. Regular expressions can help carefully specify which files to rename and then rename them with complex rules. If you have thousands of files to rename following some set of guidelines, this is invaluable. It would be all but impossible to do the same thing by hand, and why would you? Computers are made to do repetitive tasks for us.
You can roll your own script for this functionality using any of the languages that support regular expressions, like Python or Perl, but there are software options available too. Check out A Better Finder Rename on macOS for batch file renaming powered by regular expressions. If you deal with a lot of computers files that need to be renamed via some kind of scheme, this app can massively improve your productivity.
2. Search spreadsheets for formatted data
If you’ve ever scrolled through a spreadsheet looking for all data formatted like a ZIP code, regular expressions will be a fast friend. Regular expressions exist to solve exactly that kind of problem, returning data formatted in a certain way. For example, if you want to find valid US ZIP codes in all three popular formats, you could use a regular expression like the following:
Let’s break it down a little:
^
indicates the start of the string\d{5}
match five digits(?:
starts a group[-\s]
matches on a space or a dash\d{4}
matches the final four digits)?
indicates the preceding group is optional$
ends the string
This expression will match five-digit ZIP codes as well as ZIP codes with the optional four-digit qualifier, with and without a dash.
Popular spreadsheet programs like Excel don’t support regular expressions right off the bat, however. You’ll need to either use a little VBScript or write a simple Python program to crawl through your data. If you’re already handy with code, you shouldn’t find this task too overwhelming.
3. Advanced find and replace
Some surprising applications offer support for finding and replacing text with regular expressions. Adobe’s InDesign offers support for regular expressions, as does Word. Each engine has its own implementation quirks, and neither could be called complete, but they’ll get the job done. In Word you could use this to rearrange the formatting for written dates or add periods to abbreviations. If you need more power, you can use a third-party tool like AquaGREP to search a variety of text documents with a fully-featured RegEx engine. This graphical user interface for the grep command line interface makes it easier and more intuitive to search through files.
4. Search for files by content
grep
might be one of the most powerful and underutilized tools available on UNIX-flavored platforms. This command-line utility supports regular expressions by default, allowing you to search the content of files with a regular expression pattern. It doesn’t always work reliably with rich text files, but for plain text, data and spreadsheets, it’s invaluable. And while most users are stuck with .doc and .xls files, you might be surprised how often you can change those into a .txt or .csv file without losing a ton of formatting. Or at least, you can use Save As… to produce plain-text files for grep to crawl.
Because macOS is built on Unix, it has built-in access to grep
through the command line. We’ve already discussed how you can get started with grep in one of our Terminal posts, and you should check that out if you’d like to learn more.
Conclusion
Once you’re used to using regular expressions, you have access to an immensely powerful tool. Any time you’re searching for something visual, stop for a moment and consider whether you could write a quick RegEx to do the same thing for you. Even if you’re not a programmer, you can get a lot of use out of regular expressions.
You might also like: