regexutil

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultTimeout = 2 * time.Second

Longest one match of a pattern from Compile may run before it gives up, the guard against catastrophic backtracking

Variables

This section is empty.

Functions

func All

func All(re *regexp2.Regexp, s string) iter.Seq[*regexp2.Match]

Successive non-overlapping matches of re in s from left to right, ending early and silently when a match runs out of time, which only a Compile pattern can do

func Compile

func Compile(pattern string, opts ...Option) (*regexp2.Regexp, error)

Pattern from outside the program (a user, a config file, a skill, a plugin, the model) compiled in the package dialect: \w \d \s \b and case folding Unicode-aware, $ also before a final line break, (?P<name>...) (?P=name) \Z {,n} possessive quantifiers \x{...} \N{NAME} and the a flag read as the patterns already written expect, plus lookaround, backreferences, atomic groups and conditionals; groups are numbered left to right and every match gives up after DefaultTimeout with an error whose text quotes the whole subject

func Find

func Find(re *regexp2.Regexp, s string) *regexp2.Match

Leftmost match of re in s, nil when there is none or the match runs out of time, which only a Compile pattern can do

func Group

func Group(m *regexp2.Match, n int) string

Text of group n of m, empty when m is nil, the group does not exist or it took no part in the match

func Lazy

func Lazy[T any](compile func(string) T, pattern string) func() T

Function that compiles pattern with compile on its first call and hands back that same value on every later call, safe for concurrent first use, keeping package level patterns out of program start-up; a compile that panics panics again with the same value on every call

func Match

func Match(re *regexp2.Regexp, s string) bool

Whether re matches somewhere in s, false when the match runs out of time, which only a Compile pattern can do: a decision that must not read a give-up as no match calls re.MatchString and handles its error

func MustCompile

func MustCompile(pattern string) *regexp2.Regexp

Pattern written in the program compiled in the dialect Compile takes but without a time limit, panicking with an *Error when it does not compile

func NamedGroup

func NamedGroup(m *regexp2.Match, name string) string

Text of the group called name in m, empty when m is nil, the pattern has no such group or it took no part in the match

func Replace

func Replace(re *regexp2.Regexp, s, template string) string

s with every match of re replaced by template, in which $1 or ${1}, ${name}, $0 and $$ stand for a group, a named group, the whole match and a dollar sign, a reference to a group the pattern lacks staying literal text and a group that took no part giving nothing; s unchanged when the template is malformed or a match runs out of time, which only a Compile pattern can do

func ReplaceFunc

func ReplaceFunc(re *regexp2.Regexp, s string, fn func(regexp2.Match) string) string

s with every match of re replaced by what fn returns for it, s unchanged when a match runs out of time, which only a Compile pattern can do

func ReplaceSubmatchFunc

func ReplaceSubmatchFunc(re *regexp.Regexp, s string, fn func(groups []string) string) string

s with every match of the standard library pattern re replaced by what fn returns for its groups, groups[0] being the whole match and a group that took no part being empty

Types

type Error

type Error struct {
	Pattern string
	Err     error
}

Pattern the package or the engine rejects, with the pattern as given and the *syntax.Error saying why

func (*Error) Error

func (e *Error) Error() string

Message naming the pattern and what is wrong with it

func (*Error) Unwrap

func (e *Error) Unwrap() error

Syntax error the engine reported

type Option

type Option func(*settings)

Setting Compile applies to the pattern it builds

func IgnoreCase

func IgnoreCase() Option

Option making letters match regardless of case

func Whole

func Whole() Option

Option making the pattern match only a whole subject, never part of one

Jump to

Keyboard shortcuts

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