regen

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: BSD-2-Clause Imports: 8 Imported by: 0

README

regen (package)

This is a fork of nilium/regen transformed into a golang package.

Usage

$ go get github.com/kekersdev/regen

Examples

Simple
package main

import (
    "fmt"

    "github.com/kekersdev/regen"
)

func main() {
    xeger := regen.FromPattern(`0x[\da-f]{16}`)
    fmt.Println(xeger.MustGenerate())
}

FromPattern() accepts a regular expression as a single parameter, parses it assuming it uses Perl-like syntax and returns a ready-to-use instance of string generator or nil in case of an error.

MustGenerate() method returns a new generated string. Will panic if an error occurs during generation.

Advanced
package main

import (
    "fmt"
    "regexp/syntax"

    "github.com/kekersdev/regen"
)

func main() {
    xeger := &XegerGenerator

    xeger.AddPattern(`0x[\da-f]{16}`,     syntax.Perl,  false)
    xeger.AddPattern(`#[[:alpha:]]{5,6}`, syntax.POSIX, true)

    xeger.SetUnboundLimit(10)

    if str, err := xeger.Generate(); err != nil {
        fmt.Println("Failed to generate string")
    } else {
        fmt.Println(str)
    }
}

AddPattern() method offers more flexibility as it allows to specify expression parser flags and wether or not parsed expression should be simplified. Note: when a generator instance holds multiple expressions, a random one will be selected when generating a string.

SetUnboundLimit() method allows to change the default maximum number of repetitions for patterns with unbound quantifiers (+/*). Note: default limit for repetitions is 32, the same as for the original regen.

Generate() method works the same as MustGenerate() but returns an error alongside the generated string. If an error occurs during generation the returned string will be empty. Note: while the original regen may panic in certain situations, Generate() will return an error instead.

License

regen is licensed under a 2-clause BSD license. This can be found in LICENSE.txt.

Documentation

Overview

Package regen is a tool for generating random strings from Go/RE2 regular expressions. It is based on homonymous CLI tool originally developed by by Noel Cower.

Index

Constants

View Source
const DefaultUnboundLimit int = 32

Default maximum number of repetitions allowed for patterns with unbound quantifiers

Variables

This section is empty.

Functions

This section is empty.

Types

type Xeger added in v1.0.2

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

A Xeger is a regex-based string generator.

The zero value for Xeger holds no patterns and has the default limit for unbound quantifiers expansion.

All Xeger methods are safe for concurrent use by multiple goroutines.

func FromPattern added in v1.0.2

func FromPattern(pattern string) *Xeger

FromPattern attempts to parse the pattern assuming it has Perl-like syntax.

Returns a ready-to-use instance of Xeger or nil if expression can not be parsed.

func (*Xeger) AddPattern added in v1.0.2

func (g *Xeger) AddPattern(pattern string, mode syntax.Flags, simplify bool) error

AddPattern attempts to parse the pattern and add it to the Xeger instance.

  • mode is used to specify parser flags
  • simplify controls whether parsed expression should be simplified using syntax.Regexp.Simplify

func (*Xeger) Generate added in v1.0.2

func (g *Xeger) Generate() (str string, err error)

Generate attempts to generate a string using a pattern from Xeger instance. If multiple patterns are held by Xeger instance, a random one is selected.

func (*Xeger) MustGenerate added in v1.0.2

func (g *Xeger) MustGenerate() string

MustGenerate attempts to generate a string using a pattern from Xeger instance. If multiple patterns are held by Xeger instance, a random one is selected.

Returns an empty string if any errors occur (including the case when Xeger instance does not hold any pattern)

func (*Xeger) SetUnboundLimit added in v1.0.2

func (g *Xeger) SetUnboundLimit(limit int) error

SetUnboundLimit is used to update the maximum number of repetitions allowed for patterns with unbound quantifiers.

Jump to

Keyboard shortcuts

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