regex

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagIgnoreCase uint32 // i
	FlagLocale            // L
	FlagMultiline         // m
	FlagDotAll            // s
	FlagUnicode           // u
	FlagVerbose           // x
	FlagDebug             // -
	FlagASCII             // a
	FlagFallback          // -

)

Possible flags for the flag parameter. See also https://docs.python.org/3/library/re.html#flags. Note, that the additional flag `FlagFallback` is specific to this Starlark implementation.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine interface {

	// Flags returns the regex flags. The flags are parsed from the regex pattern and
	// correspond to the regex flags of the Python re module.
	Flags() uint32

	// SubexpNames returns a slice of subexpression names. The first element represents
	// the entire regex pattern and always contains an empty string. The i-th element
	// represents the i-th group. If the i-th group is not named, the i-th element
	// in the slice is the empty string.
	// The resulting slice should not be modified.
	SubexpNames() []string

	// SubexpCount returns the number of parenthesized subexpressions in the regex pattern.
	SubexpCount() int

	// SubexpIndex returns the index of the subexpression with the given name, or -1 if
	// there is no subexpression with that name.
	SubexpIndex(name string) int

	// SupportsLongest returns, if the regex engine supports the longest match search.
	// Currently, the longest match search is only supported by `regexp.Regexp`.
	SupportsLongest() bool

	// BuildInput creates a input object that is used for searching the regex pattern.
	// The regex engines work differently in terms of match positions and how matches
	// are found in strings with illegal UTF-8 code points. So, before passing the
	// string to the regex engine, the search string has to be modified, and afterwards
	// the positions of matches have to be adjusted. To improve performance, the search
	// string is only be processed once and a new object is created for regex search
	// operations. A maximum of `endpos` bytes are extracted from `s`. `endpos` must be
	// within the range of `[0, len(s)]`.
	BuildInput(s string, endpos int) Input
}

Engine is a wrapper type for the regex engine. Currently, the regex engines `regexp.Regexp` (see https://pkg.go.dev/regexp) and the fallback engine `regexp2.Regexp` (see https://pkg.go.dev/github.com/dlclark/regexp2) are supported. Since these engines work differently and do not share a common interface, wrapper functions are available.

func Compile

func Compile(pattern string, isStr bool, flags uint32, fallbackEnabled bool) (Engine, string, error)

Compile compiles the Python-compatible regex pattern and return a regex engine. If the fallback engine (`regexp2.Regexp`) is enabled and either unsupported subpatterns exist or the FALLBACK flag is enabled, then the fallback engine is used. Otherwise, the preprocessed regex pattern is compiled using the default regex engine (regexp.Regexp). If the DEBUG flag is enabled, the second return value is be a debug description of the parsed regex pattern.

type Input

type Input interface {

	// Find searches the input for the next match starting at position `pos`.
	// If `longest` is set to true, the search prioritizes the longest match. In case
	// the longest match search is unsupported by the regex engine, a regular search is
	// performed instead. The match is returned as a slice with 2*(n+1) elements, where
	// n is the number of capture groups. The element at index 2*i represents the
	// starting position of capture group i and index 2*i+1 represents the ending position,
	// where i = 0 corresponds to the entire match. The positions are returned as byte
	// offsets in the string, So, if `s` is the search string and `start` and `end` are
	// the start and end positions of the match, then the matched portion is `s[start:end]`.
	// If group i wasn't matched, then both values are -1. It's recommended to use the
	// `dstCap` parameter as the output slice.
	Find(pos int, longest bool, dstCap []int) ([]int, error)
}

Input is the type to perform a regex search.

type TemplateRule

type TemplateRule struct {
	Literal string
	Group   int
}

TemplateRule is a rule of the template. A single template rule can either represent a literal or a group index. If the group index is -1, this rule is interpreted as a literal.

func ParseTemplate

func ParseTemplate(r Engine, template string, isString bool) ([]TemplateRule, error)

ParseTemplate converts a template string into a list of rules. This list is a sequence of literal rules and group rules. For example, the template "x\1\2yz\3" results in the following list:

  • 0: literal "x"
  • 1: group 1
  • 2: group 2
  • 3: literal "yz"
  • 4: group 3

func (*TemplateRule) IsLiteral

func (t *TemplateRule) IsLiteral() bool

IsLiteral returns, if the rule represents a literal or a group index.

Jump to

Keyboard shortcuts

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