Tools We Love: Regex101.com

The tool that we love that we’ll be highlighting today is Regex101.com, which can help you build and test regular expressions.

Regular expressions were one of the hardest things to get my head around when I first encountered them.  They appeared alien (still do sometimes), and applying them to a real-world situation at the time was difficult. I tried to “get” regular expressions for a long time with many failed attempts.

While I still don’t have the language anywhere near mastered, and wouldn’t even joke about being an expert in it, using the Regex101.com really helped me get over a lot of hurdles using the language for my day-to-day work.

While this post won’t teach you regular expressions, it will give you some tips on using Regex101.com to learn and build functioning expressions.

What Can You Do With Regular Expressions?

You don’t have to learn regular expressions. Even as an SEO, you do not need to know how to use the language; it’s not required. There isn’t much you can’t do without regular expressions. Whether you can do them as quickly or effectively is up for debate.

Regular expressions can do a few things well, but if you do not need to do those things then learning regular expressions might be a waste of time (outside of merely for the love of learning which we do have here at UpBuild.) Here are some things regular expressions are good at:

  • File renaming
  • Text search
  • Web directives (like URL rewrites)
  • Database queries
  • Search and replace
  • Searching in Google Analytics can be easier
  • Creating more advanced tags and triggers in Google Tag Manager

Regular expressions can do more than what is listed above, but if any of the tasks above are things you encounter regularly, you might want to learn regular expressions.

What is Regex101.com?

Regex101.com is an interactive regular expression console that lets you debug your expressions in real-time. What this means is that you can build your expressions and see how it affects a live data set all in one screen at the same time.

The tool was created by Firas Dib, with contributions from many other developers. It is said to be the largest regex testing service in the world.

Why We Love Regex101.com

Regex101.com lets us build expressions fast and debug along the way. We can paste in a set of data and then, through trial and error, build a expression that does what we want.

The tool makes it clear if data is matching our expression or not.

In this example we are trying to match keywords with three consecutive “l” characters but no matches were found because no strings in our test area have three consecutive “l” characters.

It even tells us when our expression is broken, and then gives us some explanation of why the expression is not working.

For example, in the image below we are trying to match the word “hello” but we are mixing “[“ and “}” characters which isn’t a valid expression.

These two feedback mechanisms are really helpful if you’re not a master of the regular expression language, or you just don’t know how to build the right expression yet. Being able to trace each step of your expression is a true lifesaver when you can’t figure out why something won’t work, or even if you’re just interested in learning more about regular expressions.

Getting this instant feedback without Regex101.com would have required me to write my expressions in a text editor and then run the code over and over, without getting much feedback about why it is or isn’t working. Regex101.com eliminates the mystery.

Not only does Regex101.com make it easy to build expressions, find errors, and even learn as you do it, they make looking up a token or character in regular expressions very easy. Always present, unless you minimize it, is a “Quick Reference” that lets you look up any token or character you need.

Finally, Regex101.com lets you switch which “flavor” or version of regular expressions you want to use. Why would you want to do that? Well, you might need to integrate your regular expression expression into any number of other programming languages such as Python, C, Golang, etc. Regex101.com lets you change the version of the testing environment you’re using and will even generate the code in that language for you to use in other projects.

In this example, we are looking for words that have two consecutive instances of the letter “l.”

Here is our “l{2}” expression in both Javascript and Python:

Javascript:

Python:

Regex101.com makes nearly every part of writing expressions quicker, easier, and an educational experience.

How We Use Regex101.com

So, how would we use Regex101.com in a normal everyday situation? Here is an example:

Let’s say I have a list of strings (such as URLs) and I need to identify all strings that match a certain character expression. In our case, we want all strings that start with “https://” and end with .io. We could go through the list of strings by hand, but if we had thousands or even hundreds of thousands of strings, it could take a very long time. This is a situation where regular expressions might be a good choice to tackle this issue. Let’s get started.

The first thing I’ll do is paste in my list of URLs, or strings, into the “Test String” window.

We’ll start building our regular expression expression and let Regex101.com help us if we run into any issues.

The first thing we’ll want to do is identify how the strings we want to match should start. We do that using the “^” or caret token. This denotes how a string should start.

Regex101.com has found 8 matches so far based just on our expression of “^”, because there are 8 total strings in our data set.

Next we’ll need to identify strings that start with our required “https://.”

Uh oh, we ran into an error. Something in our expression isn’t working. Luckily, we’re not alone in this. Regex101.com lets us know that the most likely issue is that we didn’t “escape” our backslash characters.

In regular expressions, some “normal” characters like /, ., or $ have different meanings in the context of a regular expression instead of just being strings. In this case, backslash, as represented by the “/” character that we use in our URL, actually serves a different function in regex. To get around this, we explicitly tell our regex engine to interpret this character as a string rather than a token. We do that by “escaping” each backslash with a forward slash, which looks like this:

\/\/

Now that we’ve escaped our backslashes in the URL, we can move on.

Next, we’ll identify the rest of the URL we want to match, and make sure that Regex101.com interprets all the characters in our string (such as .) as strings and that the string ends with “.io.”

Remember how we used the caret or “^” in our expression to set how each string should start? The “$” does the same thing, but marks where the string should end; in our case, after “.io.” The “^” and “$” are called anchors, and allow you to specify where a expression should start and end; they are extremely useful!

We’re done! Out of our initial list of strings, we found one exact match.

Once you start getting the hang of regular expressions, you’ll be amazed at all the uses you find for them. Even RegEx pros can get a lot of use out of a troubleshooting tool like RegEx101, though. What are your favorite ways to use regular expressions? Let us know in the comments!

Related Posts

Leave a Reply

Your email address will not be published.