MCPcopy Index your code
hub / github.com/curious-odd-man/RgxGen

github.com/curious-odd-man/RgxGen @3.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 3.1 ↗ · + Follow
802 symbols 2,842 edges 123 files 60 documented · 7%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Regex: generate matching and non-matching strings

This is a java library that, given a regex pattern, allows to:

  1. Generate matching strings
  2. Iterate through unique matching strings
  3. Generate not matching strings

Table of contents

Status

Try it now

Usage

Supported Syntax

Configuration

Limitations

Other similar libraries

Support

Status

License Maven Central javadoc

Build status:

Latest Release Latest snapshot
Build Status Build Status
codecov codecov

Try it now!!!

Follow the link to Online IDE with created project: JDoodle. Enter your pattern and see the results.

Usage

Maven dependency

The Latest RELEASE:

mvnrepository.com


<dependency>
    <groupId>com.github.curious-odd-man</groupId>
    <artifactId>rgxgen</artifactId>
    <version>3.1</version>
</dependency>

Code:

Note - RgxGen is not thread safe - there were reports on errors - see #91.

public class Main {
    public static void main(String[] args) {
        RgxGen rgxGen = RgxGen.parse("[^0-9]*[12]?[0-9]{1,2}[^0-9]*");       // Create generator
        String s = rgxGen.generate();                                        // Generate new random value
        Optional<BigInteger> estimation = rgxGen.getUniqueEstimation();      // The estimation (not accurate, see Limitations) how much unique values can be generated with that pattern.
        StringIterator uniqueStrings = rgxGen.iterateUnique();               // Iterate over unique values (not accurate, see Limitations)
        String notMatching = rgxGen.generateNotMatching();                   // Generate not matching string
    }
}
public class Main {
    public static void main(String[] args) {
        RgxGen rgxGen = RgxGen.parse("[^0-9]*[12]?[0-9]{1,2}[^0-9]*");       // Create generator
        Random rnd = new Random(1234);
        String s = rgxGen.generate(rnd);                                     // Generate first value
        String s1 = rgxGen.generate(rnd);                                    // Generate second value
        String s2 = rgxGen.generate(rnd);                                    // Generate third value
        String notMatching = rgxGen.generateNotMatching(rnd);                // Generate not matching string
        // On each launch s, s1 and s2 will be the same
    }
}

Supported syntax

Supported syntax

Pattern Description
. Any symbol. See below details - Dot pattern generated symbols section.
? One or zero occurrences
+ One or more occurrences
* Zero or more occurrences
\r Carriage return CR character
\t Tab character
\n Line feed LF character.
\d A digit. Equivalent to [0-9]
\D Not a digit. Equivalent to [^0-9]
\s Configurable. By default: Space or Tab. See WHITESPACE_DEFINITION property.
\S Anything, but Carriage Return, Space, Tab, Newline, Vertical Tab, Form Feed
\w Any word character. Equivalent to [a-zA-Z0-9_]
\W Anything but a word character. Equivalent to [^a-zA-Z0-9_]
\i Places same value as capture group with index i. i is any integer number.
\Q and \E Any characters between \Q and \E, including metacharacters, will be treated as literals.
\b and \B These characters are ignored. No validation is performed!
\xXX and \x{XXXX} Hexadecimal value of unicode characters 2 or 4 hexadecimal digits
\uXXXX Hexadecimal value of unicode characters 4 hexadecimal digits
\p{...} Any character in class. See details below before use.
\P{...} Any character not in class. See details below before use.
{a} and {a,b} Repeat a; or min a max b times. Use {n,} to repeat at least n times.
[...] Single character from ones that are inside brackets. [a-zA-Z] (dash) also supported
[^...] Single character except the ones in brackets. [^a] - any symbol except 'a'
(...) To group multiple characters for the repetitions
(?<name>...) Named group
foo(?=bar) and (?<=foo)bar Limited support. Positive lookahead and lookbehind. These are equivalent to foobar. Please see Lookahead and Lookbehind section.
foo(?!bar) and (?<!foo)bar Limited support. Negative lookahead and lookbehind. Please see Lookahead and Lookbehind section.
(a|b) Alternatives
\ Escape character (use \\ (double backslash) to generate single \ character)

RgxGen treats any other characters as literals - those are generated as is.

Configuration

RgxGen can be configured per instance.

Please refer to the following enum for all available properties: com.github.curiousoddman.rgxgen.config.RgxGenOption.

Create and Use Configuration Properties

Use new RgxGenProperties() to create properties object.

Code example

public class Main {
    public static void main(String[] args) {
        // Create properties object (RgxGenProperties extends java.util.Properties)
        RgxGenProperties properties = new RgxGenProperties();
        // Set value "20" for INFINITE_PATTERN_REPETITION option in properties
        RgxGenOption.INFINITE_PATTERN_REPETITION.setInProperties(properties, 20);
        // ... now properties can be passed to RgxGen
        RgxGen rgxGen_3 = RgxGen.parse(properties, "my-cool-pattern");
    }
}

Limitations

Dot pattern generated symbols

In regex dot . means any symbol.

By default, this would generate any value in a range defined in ASCII_SYMBOL_RANGE here com.github.curiousoddman.rgxgen.parsing.dflt.ConstantsProvider.java i.e.: any character starting from space to ~.

You can modify range of allowed values using DOT_MATCHES_ONLY configuration property.

For example:

public class Main {
    public static void main(String[] args) {
        RgxGenProperties properties = new RgxGenProperties();
        RgxGenOption.DOT_MATCHES_ONLY.setInProperties(properties, RgxGenCharsDefinition.of("abc"));
        RgxGen rgxGen = RgxGen.parse(properties, ".");
        String generatedValue = rgxGen.generate();      // Will produce either "a" or "b" or "c".
    }
}

Lookahead and Lookbehind

Currently, these two have very limited support. Please refer to #63. I'm currently working on the solution, but I cannot say when I come up with something.

Estimation

rgxGen.getUniqueEstimation() - might not be accurate, because it does not count actual unique values, but only counts different states of each building block of the expression. For example: "(a{0,2}|b{0,2})" will be estimated as 6, though actual number of unique values is 5. That is because left and right alternative can produce same value. At the same time "(|(a{1,2}|b{1,2}))" will be correctly estimated to 5, though it will generate same values.

Uniqueness

For the similar reasons as with estimations - requested unique values iterator can contain duplicates.

Infinite patterns

By design a+, a* and a{n,} patterns in regex imply infinite number of characters should be matched. When generating data, that would mean values of infinite length might be generated. It is highly doubtful anyone would require a string of infinite length, thus I've artificially limited repetitions in such patterns to 100 symbols, when generating random values. This value can be changed - please refer to configuration section.

On the contrast, when generating unique values - the number of maximum repetitions is Integer.MAX_VALUE.

Use a{n,m} if you require some specific number of repetitions. It is suggested to avoid using such infinite patterns to generate data based on regex.

Not matching values generation

The general rule is - I am trying to generate not matching strings of same length as would be matching strings, though it is not always possible. For example pattern . - any symbol - would yield empty string as not matching string. Another example a{0,2} - for this pattern not matching string would be an empty string, but I would only generate the resulting strings of 1 or 2 symbols long. I chose these approaches because they are predictable and, probably, desirable for users.

Which values are used in non-matching generation

Whenever non-matching result is requested, with either `RgxGen.

Extension points exported contracts — how you extend this code

NodeTreeBuilder (Interface)
Interface for the parser/nodes builder. [18 implementers]
src/main/java/com/github/curiousoddman/rgxgen/parsing/NodeTreeBuilder.java
DataInterface (Interface)
(no doc) [2 implementers]
src/test/java/com/github/curiousoddman/rgxgen/data/DataInterface.java
NodeVisitor (Interface)
(no doc) [24 implementers]
src/main/java/com/github/curiousoddman/rgxgen/visitors/NodeVisitor.java
StringIterator (Interface)
(no doc) [22 implementers]
src/main/java/com/github/curiousoddman/rgxgen/iterators/StringIterator.java
NodeCreator (Interface)
(no doc) [2 implementers]
src/main/java/com/github/curiousoddman/rgxgen/parsing/NodeCreator.java
CharPredicate (Interface)
(no doc) [2 implementers]
src/main/java/com/github/curiousoddman/rgxgen/util/chars/CharPredicate.java

Core symbols most depended-on inside this repo

range
called by 1106
src/main/java/com/github/curiousoddman/rgxgen/model/SymbolRange.java
of
called by 139
src/main/java/com/github/curiousoddman/rgxgen/model/RgxGenCharsDefinition.java
keys
called by 125
src/main/java/com/github/curiousoddman/rgxgen/model/UnicodeCategory.java
add
called by 80
src/main/java/com/github/curiousoddman/rgxgen/util/chars/CharList.java
get
called by 65
src/main/java/com/github/curiousoddman/rgxgen/parsing/NodeTreeBuilder.java
parse
called by 60
src/main/java/com/github/curiousoddman/rgxgen/RgxGen.java
getPattern
called by 47
src/test/java/com/github/curiousoddman/rgxgen/data/DataInterface.java
get
called by 45
src/main/java/com/github/curiousoddman/rgxgen/util/chars/CharList.java

Shape

Method 665
Class 125
Enum 6
Interface 6

Languages

Java100%

Modules by API surface

src/main/java/com/github/curiousoddman/rgxgen/parsing/dflt/DefaultTreeBuilder.java29 symbols
src/test/java/com/github/curiousoddman/rgxgen/util/InvertRangesTests.java19 symbols
src/test/java/com/github/curiousoddman/rgxgen/manual/generator/unicode/UnicodeCategoryGeneration.java19 symbols
src/main/java/com/github/curiousoddman/rgxgen/util/chars/CharList.java19 symbols
src/main/java/com/github/curiousoddman/rgxgen/nodes/SymbolSet.java19 symbols
src/test/java/com/github/curiousoddman/rgxgen/parsing/dflt/CharIteratorTests.java17 symbols
src/test/java/com/github/curiousoddman/rgxgen/model/UnicodeCategoryTest.java17 symbols
src/test/java/com/github/curiousoddman/rgxgen/data/TestPattern.java17 symbols
src/test/java/com/github/curiousoddman/rgxgen/iterators/IteratorResettingTests.java16 symbols
src/main/java/com/github/curiousoddman/rgxgen/util/chars/CharArrayList.java16 symbols
src/test/java/com/github/curiousoddman/rgxgen/util/chars/CharArrayListTest.java15 symbols
src/main/java/com/github/curiousoddman/rgxgen/parsing/dflt/CharIterator.java15 symbols

For agents

$ claude mcp add RgxGen \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page