RegExr makes regular expressions easy(er)

Updated on:

This post is a bit of a departure from the usual material on this site, as it focuses on a new programming tool that I ran across, but I hope that it can be of interest to non-programmers as well. Some of this post is a little technical, but it also describes a website that helps to simplify one of the more convoluted elements common to many programming tasks. On top of that, the element of focus (regular expressions) can be a useful tool for anyone who is interested in performing more complex search or search-and-replace functions, be it in the context of programming or not.

Regular expressions are an extremely useful programming tool for finding and manipulating patterns in text. Unfortunately, the syntax can be pretty overwhelming if you've never used them before (or even if you have). Happily, website RegExr.com provides a great way to build and test regular expressions. If you've never run across regular expressions before, you can basically think of them as search-and-replace on steriods (or you can read up on them at wikipedia or regular-expressions.info). For example, regular expressions make it easy to find any particular configuration of numbers and separators, such as those that might commonly define a date (e.g., YYYY.MM.DD, where M, D, and Y can be any digit). Furthermore, they let you keep and manipulate the matches to rewrite them in a different form (e.g. converting the text to contain MM-DD-YY).

Regular expressions can be extremely powerful and are useful in a number of different contexts. Unfortunately, even simple regular expressions may look rather confusing. For example, the regular expression for the date swap mentioned above looks like:

s/\b\d{2}(\d{2})\-(\d{2})\-(\d{2})\b/$2-$3-$1/g

RegExr doesn't go quite so far as to generate this seemingly complicated command (although, trust me, if you work through one of the tutorials linked above, it won't seem quite so complicated anymore), but it does provide an easy lookup for some commonly used expressions, and also lets you instantly see the results of your expression on some sample text. While I may have butchered that explanation, a screenshot should immediately demonstrate the usefulness of the site to anyone who has ever struggled with trouble shooting a regular expression.

RegExr lets you input the match expression, the replacement (optionally), and some text to run the expression on. All matches are highlighted in real time and the modified text is returned. Finally, the full regular expression and captured groups (those enclosed in parentheses for reuse) are displayed at the bottom, along with any flags used (e.g. 'g' for global search, or 'i' for case insensitive). For the example shown here, you can see that the expression matches and reformats three different dates in the text. The panel on the right shows a number of different samples, along with descriptions. It also has some saved expressions for common tasks such as formatting SQL queries or validating email addresses.

Anyway, if you've already used regular expressions in the past, hopefully you have an idea of how helpful RegExr can be. If you haven't, hopefully this simple example gives you a hint of how powerful they are. With even just a basic tutorial and RegExr.com, you could reasonable perform some rather sophisticated text manipulations in an automated manner. Once you've learned a little about regular expressions, you'll also see them start to crop up all over the place, from the source code of web programs, to the search box of many text editors.


Sign up to receive updates in your inbox

We'll send you about two emails per month with tips on how to optimize your LACRM account, and grow your small business. Be the first to hear about product updates, and beta testing opportunities!