regexpand

package module
v0.0.0-...-adaee2e Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Regexpand

Purpose

regexpand is a Golang library that provides information about the strings a regular expression matches.

It can be used to guess type information from regular expessions, turn field validation into dropdowns...

Usage

See godoc for more.

Expand()
const maxOutputLen = 100
res, _ := regexpand.Expand("(?i:p)u[m]p(kin|)s?", maxOutputLen)
fmt.Printf("%+v\n", res)
// Output:
// [Pump Pumpkin Pumpkins Pumps pump pumpkin pumpkins pumps]
ASCIIRange()
res, _ := regexpand.ASCIIRange(`\d+`)
fmt.Println(res)
// Output:
// [0-9]

This is not an officially supported Google product. This project is not eligible for the Google Open Source Software Vulnerability Rewards Program.

Documentation

Overview

Package regexpand provides a way to expand a regex into a list of strings that it matches.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrorInfinite = errors.New("infinite cardinality")

ErrorInfinite is returned when the regex matches an infinite number of strings.

View Source
var ErrorLargeResult = errors.New("large cardinality")

ErrorLargeResult is returned when the regex would result in a large number of strings.

View Source
var ErrorUnsupported = errors.New(`regex unsupported`)

ErrorUnsupported is returned when the regex is not supported.

In practice this can happen when:

  • a regex contains an alternate with mismatching anchors, such as (a$|b). On the other hand, (a$|b$) and (a|b)$ are supported.
  • a regex contains begin of words or end of words anchors such as \b or \B.

Functions

func Expand

func Expand(regex string, maxLen int) ([]string, error)

Expand takes a regex and returns a list of strings that the regex matches.

Expand operates in O(len(re)*maxLen) time, thus it fails if the regex is too complex and returns ErrorUnsupported. If the number of strings is larger than maxLen, Expand returns ErrorLargeResult. If the regex matches an infinite number of strings, Expand returns ErrorInfinite. If the regex matches an invalid rune, it is substituted with RuneInvalid.

Example
package main

import (
	"fmt"

	"github.com/google/regexpand"
)

func main() {
	const maxOutputLen = 100
	res, _ := regexpand.Expand(`(?i:p)u[m]p(kin|)s?`, maxOutputLen)
	fmt.Printf("%+v\n", res)
}
Output:

[Pump Pumpkin Pumpkins Pumps pump pumpkin pumpkins pumps]

Types

type ASCIISet

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

ASCIISet is a set of ASCII characters.

func ASCIIRange

func ASCIIRange(regex string) (res *ASCIISet, ok bool)

ASCIIRange returns the set of ASCII characters that are matched by the given regex.

If the regex contains non-ASCII characters, ASCIIRange returns false.

Example
package main

import (
	"fmt"

	"github.com/google/regexpand"
)

func main() {
	res, _ := regexpand.ASCIIRange(`\d+`)
	fmt.Println(res)
}
Output:

[0-9]

func (*ASCIISet) Contains

func (s *ASCIISet) Contains(r rune) bool

Contains reports whether the set contains the given rune.

func (*ASCIISet) String

func (s *ASCIISet) String() string

Jump to

Keyboard shortcuts

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