regexrand is a library for generating matching string from a Go RE2 regular expression.
regexrand can be installed using go get:
go get github.com/multimikael/regexrandGenerate a string by calling GenerateMatch(&b, re, moreLimit)
bis astrings.Builderwhere the result will be stored. The accumulated string of the builder can be returned by callingb.String().reis a givensyntax.Regexpregular expression. This can be created usingsyntax.Parse.moreLimitis anintthat determines the limit of "or more" operators. Using "or more" operator will generate a random integer between a minimum value andmoreLimit.
- Any character operators only uses ASCII 32 to 126. Same applies for not char class operators.
- End of text does not contain EOF, it just stops the futher writing to the builder.
- No Match operator and Word Boundary is unsupported.
Here is a simple example of regexrand. This will print a string with a lowercase character and 1 or more (up to 10) digits between 1 and 9.
package main
import (
"fmt"
"regexp/syntax"
"strings"
"github.com/multimikael/regexrand"
)
func main() {
re, err := syntax.Parse(`[a-z][1-9]+`, syntax.Perl)
if err != nil {
panic(err)
}
var b strings.Builder
regexrand.GenerateMatch(&b, re, 10)
fmt.Println(b.String())
}