RegexStringGenerator is a versatile Java library designed to generate random text that matches specified regular expressions.
This project is robust against infinite regular expressions, offering introspection of the expression as an automaton with finite or infinite steps.
This tool is invaluable for developers and testers who require dynamically generated data that adheres to specific patterns, such as creating unique IDs, keys under constraints, or any custom text format necessary for testing environments or data masking.
- Dynamic Text Generation: Produce strings precisely matching the nuances of any given regex pattern.
- Expression introspection: Ability to analyze the regex as an automaton and obtain information such as the cardinality of steps
- Length Constraints: Ability to specify exact length requirements for the generated strings, ensuring compliance with data standards or testing conditions.
- Property Extraction: Before generating text, the library can analyze a regex to predict the properties of the text, such as potential length and structure. This pre-generation analysis helps in understanding the feasibility and limits of the regex pattern.
- Automaton Insights: Retrieve underlying automaton properties, such as the number of states, which can be crucial for debugging or optimizing regex patterns.
- Versatile Use Cases: Ideal for generating test data, user simulation inputs, or any scenario where pattern-specific string creation is needed. Particularly useful for generating structured data like IDs, codes, or user credentials under specific constraints.
To integrate RegexStringGenerator into your Maven project, add the following dependency to your pom.xml
file:
<dependency>
<groupId>io.github.yayakm</groupId>
<artifactId>regex-string-generator</artifactId>
<version>1.0.0</version>
</dependency>
Use the following code to generate random text based on a simple regular expression:
import io.github.yayakm.core.RegexStringUtility;
public class Example {
public static void main(String[] args) {
String regex = "[a-z]{10}";
String generatedText = RegexStringUtility.generateString(regex);
System.out.println("Generated Text: " + generatedText);
}
}
Use the following code to generate text with specific length constraints:
import io.github.yayakm.core.StringGenerator;
import io.github.yayakm.config.RandomStringGeneratorBuilder;
public class Example {
public static void main(String[] args) {
// Create a generator with specific regex and length constraints using a builder pattern.
StringGenerator generator = RandomStringGeneratorBuilder.builder()
.setRegExp("[a-z]{10}-[a-z]{5,10}")
.setGlobalMaxLength(30)
.setRandom(new Random())
.build();
// Generate and print a text of exact length 10 matching the regex "[a-z]{10}".
System.out.println(generator.generateString());
}
}
import io.github.yayakm.core.RegexStringGenerator;
import io.github.yayakm.core.StringGenerator;
public class Example {
public static void main(String[] args) {
// Example for generating a very large text, commented out to avoid execution delays or memory issues.
// Uncomment the following lines to test with very large lengths.
StringGenerator generator4 = new RegexStringGenerator("[0-9]{1000}-[0-9]{50,100}");
System.out.println(generator4.generateString(1051,1051));
}
}
import io.github.yayakm.core.RegexStringGenerator;
import io.github.yayakm.core.StringGenerator;
import java.util.Random;
public class Example {
public static void main(String[] args) {
// Using the generator to create different text configurations and generate outputs.
StringGenerator generator2 = new RegexStringGenerator(new Random());
generator2.setRegExp("[a-z]");
String text1 = generator2.generateString(10, 20);
System.out.println(text1);
}
}
Use the following code to extract Property:
import io.github.yayakm.core.RegexStringUtility;
import io.github.yayakm.config.AutomatonProperties;
public class Example {
public static void main(String[] args) {
AutomatonProperties automatonProperties = RegexStringUtility.getAutomatonProperties("[a-z]{10}-[a-z]{5,10}");
}
}
Contributions to this project are welcome. If you would like to contribute, please follow these steps:
- Fork the project on GitHub.
- Create a branch for your changes.
- Commit your changes.
- Push your branch and open a Pull Request..
Distributed under the Apache License 2.0, this project is free to use, modify, and distribute.
For assistance or to report a problem, please open a ticket in the Issues section of the GitHub repository.
This file now contains two Java code examples directly included, showing how to use the library to generate text based on simple regular expressions and with length constraints.