PatternValueGenerator

class PatternValueGenerator(options: PatternGenerationOptions = PatternGenerationOptions())

Generates string values based on regular expression patterns using the regexp-gen library.

This class encapsulates the Cornutum regexp-gen library for generating strings that either match or do not match a given ECMA-262 (JavaScript-style) regular expression pattern.

Usage

// Use default options
val generator = PatternValueGenerator()
val value = generator.generateValidValue("^[A-Z]{3}$", null, null, 0)

// Or use the shared default instance
val value = PatternValueGenerator.getDefault().generateValidValue("^[A-Z]{3}$", null, null, 0)

// Custom options
val customGenerator = PatternValueGenerator(PatternGenerationOptions(defaultMinLength = 10))

Options

Generation behavior is controlled by PatternGenerationOptions passed to the constructor:

Library Limitations

The underlying regexp-gen library has the following limitations:

  • No negative lookahead/lookbehind: Patterns like (?!...) or (?<!...) are not supported

  • No word boundary assertions: \b and \B are not supported

  • No backreferences: Patterns referencing earlier capture groups are not supported

  • Some patterns accept all strings: For patterns like .*, generating non-matching strings is impossible

When these limitations are encountered, the generator will return null and log a warning.

Determinism:

  • Valid generation uses variationIndex as the random seed.

  • Invalid generation uses a fixed seed and applies length hints only.

See also

Constructors

Link copied to clipboard
constructor(options: PatternGenerationOptions = PatternGenerationOptions())

Functions

Link copied to clipboard
fun generateInvalidValue(pattern: String, minLength: Int?, maxLength: Int?): String?

Generates a string that does NOT match the given pattern.

Link copied to clipboard
fun generateValidValue(pattern: String, minLength: Int?, maxLength: Int?, variationIndex: Int): String?

Generates a string that matches the given pattern, respecting length constraints.