Skip to content

Introducing the regex-tester library for Java

February 8, 2014

regex-tester version 0.1 is an open source project that removes the boiler-plate code needed to test regular expressions with JUnit.

Regular expressions often contain business logic that is important to an application yet are rarely put through rigorous, automated testing. That’s unfortunate because it’s generally so easy to test a regular expression (regex, from here on). In many cases, you just want to know that a given string produces a match when the regex  is applied to it. That’s easy even without regex-tester but tested so infrequently in real software.

Running the regex against a handful of strings in a JUnit test is superior coverage to none at all. That’s easy to do with regex-tester:

public class BasicRegexTest {

    public static List<RegexTestStringInfo> getTestParameters() {
        return Arrays.asList(new RegexTestStringInfo[] {
                new RegexTestStringInfo(true, "com"),
                new RegexTestStringInfo(true, "com.thewonggei"),
                new RegexTestStringInfo(true, "com.thewonggei.regexTester"),
                new RegexTestStringInfo(false, ".com.thewonggei"),
                new RegexTestStringInfo(false, "")

    public void test() {}

Running the test suite above will automatically create and execute a test case for each test string specified. Even that is a lot of typing for when you want better coverage of the regex. If you want to test many more strings, you can put them in a simple properties file like this:

#A couple of out-of-range examples first
#These are just the wrong format

#However many test strings you desire

#A whole bunch of matching strings

Then the JUnit test gets even simpler:

@Regex(value="^(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$")
public class DateFormatRegexTest {

    public void test() {}


In both cases, the crucial lines of code declare the important pieces under test: the specialized JUnit test suite to use (@RegexTestSuite), the regex to test with (@Regex) and the strings to execute against the regex (either the @RegexTestStrings method or @RegexTestStringsFile). For each string, a boolean value is supplied that indicates whether or not the regex should produce a match.

Therefore, the meaning of

RegexTestStringInfo(true, "com")

is “the given regex will produce a match against the string ‘com'”. If using the properties file

means “the given regex will not produce a match against the string ‘'”.

These two examples show both the library’s current capabilities and all that is required of you to start automating the testing of your regular expressions. There is much yet to do (e.g. I know you already wondered why you can’t test for the number and content of matches in regular expressions with groups defined) that will come out in later versions. For now, I’m anxious to know about any usage of the library and, especially, what you think needs added or fixed. Feel free to open up an issue on GitHub for any usage problems, bugs or feature requests.

Get the Code

The code is under the MIT License and is hosted on GitHub here. See the project’s README file for more details.

Get the JARs

Maven coordinates:


Or, download the JARs directly from Maven Central here.

One Comment
  1. nyancode permalink

    Thank you for sharing. You can take a look at the regex tester here too. I think the diagram explainer using Javascript is quite amazing!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: