Greex Regular Expression Match Generator

Greex is a Java library that can be used to generate matches for regular expressions. It supports both random generation as well as complete generation. This can be used for creating test data, generating random tokens, or creating unit test cases.

Getting Started

To get started, you can add Greex to your pom.xml or build.gradle, or you can download the JAR files manually from the Library Archives.

Note: If you download the JAR file and add it to your classpath manually, you'll also need to download the dk.brics.automaton library and add it to your classpath as well.

Maven

Add this to your pom.xml to add Greex as a dependency:

<dependency>
    <groupId>com.navigamez</groupId>
    <artifactId>greex</artifactId>
    <version>1.0</version>
</dependency>

Gradle

Add this to your build.gradle to add Greex as a dependency:

compile 'com.navigamez:greex:1.0'

Using the Generator

The GreexGenerator class is the entry point into match generation.

GreexGenerator generator = new GreexGenerator("(white|black)|((light|dark) )?(red|green|blue|gray)");
String match = generator.generateRandom();
System.out.println(match); // e.g. "dark red"

This example uses generateRandom() to create a random match. You can also generate all possible matches with generateAll(), but this shouldn't be used for non-finite regular expressions unless a max length is provided.

GreexGenerator generator = new GreexGenerator("(white|black)|((light|dark) )?(red|green|blue|gray)");
List<String> matches = generator.generateAll();
System.out.println(matches.size()); // "14"

As documented in the API docs, these methods can all throw a StackOverflowError if the code recurses too deeply. In addition, if the maximum length passed in to generateAll(int) is too large, you may also get a OutOfMemoryError.

Further Reading

The API docs are a good starting point for reading up on the various overloads available for the generate methods as well as the caveats around thread safety.