Package regen is a library for generating random strings from regular expressions. The generated strings will match the expressions they were generated from.



will return a lowercase alphanumeric string between 1 and 64 characters long.

Expressions are parsed using the Go standard library's parser:


"." will generate any character, not necessarily a printable one.

"x{0,}", "x*", and "x+" will generate a random number of x's up to an arbitrary limit. If you care about the maximum number, specify it explicitly in the expression, e.g. "x{0,256}".


Flags can be passed to the parser by setting them in the GeneratorArgs struct. Newline flags are respected, and newlines won't be generated unless the appropriate flags for matching them are set.

E.g. Generate(".|[^a]") will never generate newlines. To generate newlines, create a generator and pass the flag syntax.MatchNL.

The Perl character class flag is supported, and required if the pattern contains them.

Unicode groups are not supported at this time. Support may be added in the future.

Running Large Generators Concurrently

A generator is usually actually tree of generators, corresponding closely to the AST of the expression. By default, generators run their children serially. In most cases, this is probably fine. However, it can be changed by passing a different GeneratorExecutor in GeneratorArgs. NewForkJoinExecutor(), for example, will cause each sub-generator to run in its own goroutine. This may improve or degrade performance, depending on the regular expression.

A large bottleneck with running generators concurrently is actually the random source. Sources returned from rand.NewSource() are slow to seed, and not safe for concurrent source. Instead, the source passed in GeneratorArgs is used to seed an XorShift64 source from the paper at This source only uses a single variable, so can be used concurrently, and is much faster to seed than the default source. One source is created per call to NewGenerator. If no source is passed in, the default source is used to seed.

Imported by 4 package(s)


Test imports 2 package(s)