How to Use Regular Expressions to Improve Productivity in Your Daily Tasks

If you are already familiar with the basic power of regular expressions, you might be interested in knowing how they can be used. If you want to brush up on the basics, check out our Regular Expressions cheatsheet and Beginner’s Guide to Regular Expressions. Here are some daily tasks where regular expressions can improve your productivity.

regular-expression-uses-rename-files

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.

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, ReNamer on Windows or Métamorphose on Linux.

regular-expression-uses-spreadsheets

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:

^\d{5}(?:[-\s]\d{4})?$

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.

regular-expression-uses-find-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 PowerGREP to search a variety of text documents with a fully-featured RegEx engine based on Perl.

regular-expression-uses-grep

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.

On macOS or Linux you have built-in access to grep through the command line. If you’re on Windows, you can use the aforementioned PowerGREP to search for files by content without replacing anything.

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.

Image credit: Find and Replace, Grep-tutorial-05

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.