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 ¶
- func Required(expr string) []string
- func Weight(lits []string) int
- type Match
- type Regexp
- func (re *Regexp) FindAllString(s string, n int) []string
- func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
- func (re *Regexp) FindAllStringMatches(s string) []*Match
- func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string
- func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
- func (re *Regexp) IsZero() bool
- func (re *Regexp) MatchStringStd(s string) bool
- func (re *Regexp) MightMatch(lowered string) bool
- func (re *Regexp) Split(s string, n int) []string
- func (re *Regexp) String() string
- func (re *Regexp) SubexpNames() []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Regexp ¶
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 MustCompile ¶
MustCompile is Compile but panics on an invalid pattern. It is meant for patterns fixed at compile time.
func (*Regexp) FindAllString ¶
FindAllString returns up to n successive matches, or all of them if n < 0.
A nil return means no match.
func (*Regexp) FindAllStringIndex ¶
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 ¶
FindAllStringMatches returns every successive match.
func (*Regexp) FindAllStringSubmatch ¶
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 ¶
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) MatchStringStd ¶
MatchStringStd reports whether s contains a match, panicking if the match itself fails.
func (*Regexp) MightMatch ¶ added in v3.17.0
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 ¶
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 ¶
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 ¶
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.