How Do I Test a Regular Expression?

After writing a brand new regular expression, it’s very important that you put it through some tests to see if it performs as you intended. How does one go about testing such an expression?

To test a regular expression, first search for errors such as non-escaped characters or unbalanced parentheses. Then test it against various input strings to ensure it accepts correct strings and regex wrong ones. A regex tester tool is a great tool that does all of this.

Testing can be done manually using your programming environment of choice, but there are some great tools online to help you accomplish this much easier. In this article, we’re going to take a deep dive into how to use a regex tester tool to test your expression.

Specifically, we’re going to use the online tool Regex101.com. Most other tools like Regexr.com and RegexTester.com work pretty much the same way, so you can follow along in the tool of your choice. Here’s what we’ll cover:

  1. Choose a regex tester tool
  2. Check your regular expression for errors
  3. Test your regular expression against input strings
  4. Ensure appropriate modifiers are turned on
  5. Test your regex using a unit tester

Throughout this article, I’ll be using an example expression that tests the validity of a base64 encoded string, to illustrate the steps:

/^(?:[A-Za-z\d+\/]{4})*(?:[A-Za-z\d+\/]{3}=|[A-Za-z\d+\/]{2}==)?$/Edit with Regexity

Choose a Regex Tester Tool

As I’ve mentioned, the best way to test your expression is with a regex tester tool. You can find various regex testers like these by just Googling “regex tester“. Start by choosing a tool you like, but one that has at least the following components:

  1. A regular expression box
    Where you paste your regular expression.
  2. A test string box
    Where you can insert a test string to see if the expression matches your test.
  3. A substitution section
    Where you can create and test regex substitutions.
  4. Modifier selector
    Where you can turn on and off various regex modifiers such as the case-insensitive modifier.
  5. Code explanation
    Generates an explanation of your code so you can make sure it’s what you intended.

ALSO READ: Regex101 vs. Regexr vs. RegexTester

Here you can see all these components as they appear in Regex101.

Getting to Know a Regex Tester Interface
Getting to Know a Regex Tester Interface

How to Check Your Regex For Errors

When testing for regular expressions, there are three types of errors you could come across:

  • Syntax errors (very obvious)
  • Character errors (less obvious)
  • Omission errors (not obvious at all)

To check a regular expression for errors, just start typing an expression into the regular expression box. Most regex testers will color your expression as you go along, making it easy to spot some errors.

Syntax Errors

Syntax errors are errors in the sequence of how characters follow one another. For example, neglecting to escape a certain character (like the forward slash character /Edit with Regexity) or forgetting a closing parenthesis )Edit with Regexity or square bracket ]Edit with Regexity will trigger syntax errors. Regex tester tools will highlight these in red and will also provide a simple error message to show you how to fix it.

For example, in the case below I forgot to include the closing parenthesis on the last non-capture group, causing the tool to highlight the corresponding opening parenthesis in red.

Regex Error Highlighting
Regex Error Highlighting

Character Errors

Character errors creep in when you type a character with the intention of matching a certain character or range, but which ends up matching something totally different. These often surface when you use some of the reserved characters without escaping them first. These include:

I cannot tell you how many times I’ve typed a period .Edit with Regexity with the intention that it will match a literal period, but forgetting that it is, in fact, a wildcard character that will match any character! The correct use case here is to escape it with a preceding backslash \.Edit with Regexity to help the regex engine interpret it correctly.

Character errors won’t be highlighted as errors in your expression, so you need to hunt for them by investigating the code explanations produced by your regex tester tool. Code explanation can often be seen by hovering your mouse over certain parts of the expression or by viewing the list-style explanation in the right-hand panel or at the bottom of the screen (depending on the tool you use).

Regex Code Explanation in Tooltip Format
Regex Code Explanation in Tooltip Format
Regex Code Explanation in List Format
Regex Code Explanation in List Format

Most tools highlight the different parts of an expression for you so you can spot these errors with a bird’s eye view. For example, groups are highlighted in green ()Edit with Regexity while ranges are highlighted in orange [A-Z]Edit with Regexity. Quantifiers are highlighted in blue {3}Edit with Regexity and escaped characters are highlighted in purple \.Edit with Regexity to discern them from regular characters.

Omission Errors

It is often said that creating a regular expression to match a few good inputs is pretty easy, but creating one that also rejects the wrong inputs is pretty hard. This type of error can creep in when you are not correctly anticipating all the variations that your matched expression might have.

These errors are nearly impossible to pick up just from code inspection, and can only be detected by means of testing input strings. And we’ll discuss this in the next section.

How to Test Your Regex Against Input Test Strings

The best use for a regex tester tool is to be able to enter a test input string and to view all the parts of the expression it matches. To do this, simply paste an input string into the test string box (see item 2 above). Most tools will highlight the expression it matches.

Test Input String
Test Input String

At this stage, it might be helpful for you to put together a list of valid and invalid input strings. For example, if you are testing for a number between 0 and 100, you might consider the following inputs:

Generate as many test strings (both valid and invalid) as you have time for. These will also come in handy when we do unit tests later on.

When you have your list of strings, paste them all into the input string box. A common practice is to paste one on each line so that you can easily differentiate between them. The tool will then highlight each input item if it is a correct match.

Test Multiple Input Strings
Test Multiple Input Strings

Do remember to enable the global flag gEdit with Regexity (use item 4 above) to allow matching all correct matches instead of just the first one. Also, if you use the start-of-string ^Edit with Regexity or end-of-string $Edit with Regexity characters in your expression, enable the multiline flag mEdit with Regexity to match the start and end of each line, instead of the entire text body.

Some tools will even color capture groups with different colors if you have any of these in your expression. These are quite useful to see if your expression will return any sub-matches.

Capture Group Highlighting
Capture Group Highlighting

Ensure the Appropriate Modifiers are Turned On

Modifiers are pretty important in regular expressions since they change the entire behavior of your expression. These include, amongst others:

Once again, regex testers make it fairly easy to experiment with various combinations of these modifiers. The toggle button to turn certain modifiers on or off usually sits right next to the regular expression box (see item 4 above). Play around with the modifiers to see how they impact the matching of your input strings.

Here’s a list of the available modifiers in Regex101 for the Javascript regex flavor.

Javascript Modifiers in Regex101
Javascript Modifiers in Regex101

How to Test Regex Using a Unit Tester

The most advanced way of testing a regular expression is by using a unit tester. Unit testers make it easy to test both valid and invalid input strings quickly and easily and get a bird’s eye view of results.

These are built into Regex101 and Regexr and are pretty easy to set up and use. In Regex101, simply click the Unit Tests button on the left hand side, and then click add test. Insert the test string along with an optional description.

At the bottom of the “Add Unit Test” box , you can specify how this test should pass. The does match option will cause the test to pass if the input string matches the expression, while the does not match option will pass if the input string does not match the expression. So valid input strings should be assigned the does match option and invalid strings the does not match option.

Unit Tester Dialog Box
Unit Tester Dialog Box

Finally, press the “Run tests” button at the bottom of the screen. All the passed tests will show up in green while the failed tests will show up in red. You can then consider each failed test and figure out why your expression did not pass this test.

Unit Test Results
Unit Test Results

Unit testers are really helpful to ensure the integrity of the entire expression. Sometimes, when you change your expression to match a certain use case better, you might unknowingly change it in a way that now rejects a correct use case. Unit testers help you keep track of all the input strings and ensure your expression has “backward compatibility”.

Conclusion

There you have it! A few quick and easy steps to test your regular expression before putting it to use in the real world. If you have any additional suggestions, please drop them in the comment box below so we can all learn from one another.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

Leave a Comment