regexamples

package
v1.32.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 Imported by: 0

README

Regex Examples (regexamples)

  • Generate: Generates n random strings that match the given regular expression pattern.
  • NewGenerator: Compiles a regex pattern into a reusable Generator, avoiding repeated parsing overhead when generating multiple batches.
  • Generator.Generate: Generates n matching strings from a pre-compiled Generator.
  • Generator.SetSeed: Sets a deterministic seed on the Generator so that subsequent calls produce the same sequence of strings.

Examples:

For examples of each function, please checkout EXAMPLES.md


Documentation

Overview

Package regexamples generates random strings that match a given regular expression.

Index

Constants

View Source
const (
	// DefaultMaxRepeat is the default cap for unbounded quantifiers (*, +, and {n,} without upper bound).
	DefaultMaxRepeat = 10
)

Variables

This section is empty.

Functions

func Generate

func Generate(pattern string, n int) ([]string, error)

Generate is a convenience function that compiles pattern and returns n matching strings. For repeated generation from the same pattern prefer NewGenerator to avoid recompiling on every call.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator holds a compiled pattern and a random source, and can produce multiple matching strings efficiently.

A Generator is not safe for concurrent use. If you need to generate strings from multiple goroutines, create a separate Generator per goroutine or protect access with a sync.Mutex.

func NewGenerator

func NewGenerator(pattern string) (*Generator, error)

NewGenerator compiles pattern and returns a Generator ready to produce matching strings. Returns an error if the pattern is invalid.

func (*Generator) Generate

func (g *Generator) Generate(n int) ([]string, error)

Generate returns n strings that each match the compiled pattern. Returns an error if n is negative or if the pattern cannot be satisfied.

func (*Generator) SetSeed

func (g *Generator) SetSeed(seed uint64)

SetSeed resets the generator's random source to a deterministic seed, which makes subsequent Generate calls produce the same sequence of strings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL