revregexp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2020 License: MIT Imports: 4 Imported by: 0

README

revregexp

GoDoc Go Report Card

Small library for randomly generating strings from a regex pattern.

Examples

Regex pattern Sample output
.* hM7R
\d+ 415
[a-d4-8]{5} a8c6c
[a-f0-9]{4}-[a-f0-9]{6} 893a-9034d1

Sample usage

import "github.com/pipe01/revregexp

str := revregexp.Reverse("Hello (World|Everyone)")

fmt.Println(str)
// Outputs "Hello World" or "Hello Everyone"

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Default = &RegexpWriter{
	MaxQuantifierLength: 5,
}

Default is an instance of RegexpWriter with the default configurtion

Functions

func Reverse

func Reverse(pattern string, flags ...syntax.Flags) (string, error)

Reverse takes a regex pattern string and returns a string that satisfies that pattern.

Example
package main

import (
	"fmt"

	"github.com/pipe01/revregexp"
)

func main() {
	str, _ := revregexp.Reverse(`\d{5}`)

	fmt.Println(str)
	// Outputs: 19347
}
Output:

func ReversePattern

func ReversePattern(pattern *syntax.Regexp) (string, error)

ReversePattern takes a parsed regex pattern and returns a string that satisfies that pattern.

Example
package main

import (
	"fmt"
	"regexp/syntax"

	"github.com/pipe01/revregexp"
)

func main() {
	r, _ := syntax.Parse(`\d{5}`, syntax.Perl)

	str, _ := revregexp.ReversePattern(r)

	fmt.Println(str)
	// Outputs: 38572
}
Output:

Types

type RegexpWriter

type RegexpWriter struct {
	MaxQuantifierLength int // The maximum length for open-ended quantifiers (*, ?, etc)
}

RegexpWriter supports converting a syntax.Regexp to a string using random values

func (*RegexpWriter) Write

func (w *RegexpWriter) Write(r *syntax.Regexp, wr StringWriter) error

type StringWriter

type StringWriter interface {
	WriteString(str string) (int, error)
	WriteRune(r rune) (int, error)
}

StringWriter is implemented by types that support writing strings and runes to them

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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