Documentation
¶
Overview ¶
Package regen is a library for generating random strings from regular expressions. The generated strings will match the expressions they were generated from. Similar to Ruby's randexp library.
E.g.
regen.Generate("[a-z0-9]{1,64}")
will return a lowercase alphanumeric string between 1 and 64 characters long.
Expressions are parsed using the Go standard library's parser: http://golang.org/pkg/regexp/syntax/.
Constraints ¶
"." 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 ¶
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.
Concurrent Use ¶
A generator can safely be used from multiple goroutines without locking.
Benchmarks ¶
Benchmarks are included for creating and running generators for limited-length, complex regexes, and simple, highly-repetitive regexes.
go test -bench .
The complex benchmarks generate fake HTTP messages with the following regex:
POST (/[-a-zA-Z0-9_.]{3,12}){3,6} Content-Length: [0-9]{2,3} X-Auth-Token: [a-zA-Z0-9+/]{64} ([A-Za-z0-9+/]{64} ){3,15}[A-Za-z0-9+/]{60}([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)
The repetitive benchmarks use the regex
a{999}
See regen_benchmarks_test.go for more information.
Index ¶
Examples ¶
Constants ¶
const DefaultMaxUnboundedRepeatCount = 4096
DefaultMaxUnboundedRepeatCount is default value for MaxUnboundedRepeatCount.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate a random string that matches the regular expression pattern. If args is nil, default values are used.
This function does not seed the default RNG, so you must call rand.Seed() if you want non-deterministic strings.
Example ¶
pattern := "[ab]{5}" str, _ := Generate(pattern) if matched, _ := regexp.MatchString(pattern, str); matched { fmt.Println("Matches!") }
Output: Matches!
Types ¶
type CaptureGroupHandler ¶
type CaptureGroupHandler func(index int, name string, group *syntax.Regexp, generator Generator, args *GeneratorArgs) string
CaptureGroupHandler is a function that is called for each capture group in a regular expression. index and name are the index and name of the group. If unnamed, name is empty. The first capture group has index 0 (not 1, as when matching). group is the regular expression within the group (e.g. for `(\w+)`, group would be `\w+`). generator is the generator for group. args is the args used to create the generator calling this function.
Example ¶
pattern := `Hello, (?P<firstname>[A-Z][a-z]{2,10}) (?P<lastname>[A-Z][a-z]{2,10})` generator, _ := NewGenerator(pattern, &GeneratorArgs{ Flags: syntax.Perl, CaptureGroupHandler: func(index int, name string, group *syntax.Regexp, generator Generator, args *GeneratorArgs) string { if name == "firstname" { return fmt.Sprintf("FirstName (e.g. %s)", generator.Generate()) } return fmt.Sprintf("LastName (e.g. %s)", generator.Generate()) }, }) // Print to stderr since we're generating random output and can't assert equality. fmt.Fprintln(os.Stderr, generator.Generate()) // Needed for "go test" to run this example. (Must be a blank line before.)
Output:
type Generator ¶
Generator generates random strings.
func NewGenerator ¶
func NewGenerator(pattern string, inputArgs *GeneratorArgs) (generator Generator, err error)
NewGenerator creates a generator that returns random strings that match the regular expression in pattern. If args is nil, default values are used.
Example ¶
pattern := "[ab]{5}" generator, _ := NewGenerator(pattern, &GeneratorArgs{}) str := generator.Generate() if matched, _ := regexp.MatchString(pattern, str); matched { fmt.Println("Matches!") }
Output: Matches!
Example (Perl) ¶
pattern := `\d{5}` generator, _ := NewGenerator(pattern, &GeneratorArgs{ Flags: syntax.Perl, }) str := generator.Generate() if matched, _ := regexp.MatchString("[[:digit:]]{5}", str); matched { fmt.Println("Matches!") }
Output: Matches!
type GeneratorArgs ¶
type GeneratorArgs struct { // Default is 0 (syntax.POSIX). Flags syntax.Flags // Maximum number of instances to generate for unbounded repeat expressions (e.g. ".*" and "{1,}") // Default is DefaultMaxUnboundedRepeatCount. MaxUnboundedRepeatCount uint // Minimum number of instances to generate for unbounded repeat expressions (e.g. ".*") // Default is 0. MinUnboundedRepeatCount uint // Set this to perform special processing of capture groups (e.g. `(\w+)`). The zero value will generate strings // from the expressions in the group. CaptureGroupHandler CaptureGroupHandler // contains filtered or unexported fields }
GeneratorArgs are arguments passed to NewGenerator that control how generators are created.