regex

package
v3.19.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package regex adapts github.com/dlclark/regexp2 to the API Vale uses.

Vale needs a backtracking engine: user-authored rules rely on lookarounds and backreferences, which regexp/syntax rejects outright. regexp2 provides that, but its matching methods return errors and its Find surface is narrower than the standard library's, so this package supplies the missing convenience layer.

Offsets are rune-based

FindAllStringIndex and friends report positions in the *rune* slice, not byte offsets. Callers converting to byte positions must keep doing so; see core.re2Loc.

Errors

regexp2 can fail at match time, most often by exceeding its backtracking budget on a pathological pattern. The wrappers here panic in that case, matching the behaviour Vale has always had. Anything that should degrade gracefully needs to call the error-returning method directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Required added in v3.17.0

func Required(expr string) []string

func Weight added in v3.17.0

func Weight(lits []string) int

weight scores a literal set by its weakest member, since the subject only has to contain one of them.

Types

type Match

type Match = regexp2.Match

Match is a single regular-expression match.

type Regexp

type Regexp struct {
	*regexp2.Regexp
	// contains filtered or unexported fields
}

Regexp is a compiled regular expression.

It embeds *regexp2.Regexp, so the engine's own API — String, Replace, ReplaceFunc, MatchString, FindStringMatch, FindNextMatch — is available directly.

func Compile

func Compile(expr string) (*Regexp, error)

Compile parses a regular expression in RE2 compatibility mode.

func MustCompile

func MustCompile(expr string) *Regexp

MustCompile is Compile but panics on an invalid pattern. It is meant for patterns fixed at compile time.

func (*Regexp) FindAllString

func (re *Regexp) FindAllString(s string, n int) []string

FindAllString returns up to n successive matches, or all of them if n < 0.

A nil return means no match.

func (*Regexp) FindAllStringIndex

func (re *Regexp) FindAllStringIndex(s string, n int) [][]int

FindAllStringIndex returns the rune-index bounds of up to n successive matches, or all of them if n < 0.

A nil return means no match.

func (*Regexp) FindAllStringMatches

func (re *Regexp) FindAllStringMatches(s string) []*Match

FindAllStringMatches returns every successive match.

func (*Regexp) FindAllStringSubmatch

func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string

FindAllStringSubmatch returns up to n successive matches and their submatches, or all of them if n < 0.

A nil return means no match.

func (*Regexp) FindAllStringSubmatchIndex

func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int

FindAllStringSubmatchIndex returns the rune-index bounds of up to n successive matches and their submatches, or all of them if n < 0.

A group that did not participate in the match is reported as -1, -1, the same convention the standard library uses.

A nil return means no match.

func (*Regexp) IsZero

func (re *Regexp) IsZero() bool

IsZero reports whether re carries no pattern.

func (*Regexp) MatchStringStd

func (re *Regexp) MatchStringStd(s string) bool

MatchStringStd reports whether s contains a match, panicking if the match itself fails.

func (*Regexp) MightMatch added in v3.17.0

func (re *Regexp) MightMatch(lowered string) bool

MightMatch reports whether lowered could contain a match.

lowered is the subject lower-cased, which the caller does once per block rather than once per rule. A false return is definitive: the pattern cannot match. A true return means the pattern still has to be run.

func (*Regexp) Split

func (re *Regexp) Split(s string, n int) []string

Split slices s around each match, returning the pieces between them.

n limits the number of pieces; n < 0 means no limit. A pattern that does not match returns s unchanged, as a single element.

func (*Regexp) String

func (re *Regexp) String() string

String returns the source text of the pattern.

The zero value is usable and reports "". Vale relies on that: an unset pattern is represented by &Regexp{} and detected with String() == "", so this must not dereference the embedded pointer.

func (*Regexp) SubexpNames

func (re *Regexp) SubexpNames() []string

SubexpNames returns the names of the parenthesized subexpressions.

The name for the first sub-expression is names[1], so for a match slice m the name for m[i] is SubexpNames()[i]. The expression as a whole cannot be named, so names[0] is always empty.

Jump to

Keyboard shortcuts

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