package regexp2

Import Path
	github.com/dlclark/regexp2/v2 (on go.dev)

Dependency Relation
	imports 17 packages, and imported by 2 packages

Involved Source Files bufferpool.go fastclock.go match.go options.go Package regexp2 is a regexp package that has an interface similar to Go's framework regexp engine but uses a more feature full regex engine behind the scenes. It doesn't have constant time guarantees, but it allows backtracking and is compatible with Perl5 and .NET. You'll likely be better off with the RE2 engine from the regexp package and should only use this if you need to write very complex patterns or require compatibility with .NET. regexp_codegen.go replace.go runner.go split.go stringprefixfilter.go
Package-Level Type Names (total 11)
/* sort by: | */
Capture is a single capture of text within the larger original string RuneIndex is the position in the underlying rune slice where the first character of captured substring was found. Even if you pass in a string this will be in Runes. RuneLength is the number of runes in the captured substring. ByteRange returns the UTF-8 byte index and byte length of the captured substring. The first call lazily caches byte offsets on shared match text, so it is not safe to call concurrently with ByteRange on another capture from the same match until the cache has been initialized. Runes returns the captured text as a rune slice String returns the captured text as a String *Capture : expvar.Var *Capture : fmt.Stringer
CompileOption configures Compile and MustCompile. RegexOptions func OptionDebug() CompileOption func OptionDisableCharClassASCIIBitmap() CompileOption func OptionIsCodeGen() CompileOption func OptionMaintainCaptureOrder() CompileOption func OptionMaxBacktrackingStackSize(n int) CompileOption func OptionMaxCachedReplaceBufferLength(n int) CompileOption func OptionMaxCachedReplacerDataBytes(n int) CompileOption func OptionMaxCachedReplacerDataEntries(n int) CompileOption func OptionMaxCachedRuneBufferLength(n int) CompileOption func Compile(expr string, options ...CompileOption) (*Regexp, error) func MustCompile(str string, options ...CompileOption) *Regexp func RegisterEngine(pattern string, engine RuntimeEngineData, options ...CompileOption)
Group is an explicit or implit (group 0) matched group within the pattern // the last capture of this group is embeded for ease of use RuneIndex is the position in the underlying rune slice where the first character of captured substring was found. Even if you pass in a string this will be in Runes. RuneLength is the number of runes in the captured substring. // captures of this group // group name ByteRange returns the UTF-8 byte index and byte length of the captured substring. The first call lazily caches byte offsets on shared match text, so it is not safe to call concurrently with ByteRange on another capture from the same match until the cache has been initialized. Runes returns the captured text as a rune slice String returns the captured text as a String *Group : expvar.Var *Group : fmt.Stringer func (*Match).GroupByName(name string) *Group func (*Match).GroupByNumber(num int) *Group func (*Match).Groups() []Group
Match is a single regex result match that contains groups and repeated captures -Groups -Capture // embeded group 0 // the last capture of this group is embeded for ease of use RuneIndex is the position in the underlying rune slice where the first character of captured substring was found. Even if you pass in a string this will be in Runes. RuneLength is the number of runes in the captured substring. // captures of this group // group name ByteRange returns the UTF-8 byte index and byte length of the captured substring. The first call lazily caches byte offsets on shared match text, so it is not safe to call concurrently with ByteRange on another capture from the same match until the cache has been initialized. GroupByName returns a group based on the name of the group, or nil if the group name does not exist GroupByNumber returns a group based on the number of the group, or nil if the group number does not exist GroupCount returns the number of groups this match has matched Groups returns all the capture groups, starting with group 0 (the full match) Runes returns the captured text as a rune slice String returns the captured text as a String *Match : expvar.Var *Match : fmt.Stringer func (*Regexp).FindNextMatch(m *Match) (*Match, error) func (*Regexp).FindRunesMatch(r []rune) (*Match, error) func (*Regexp).FindRunesMatchStartingAt(r []rune, startAt int) (*Match, error) func (*Regexp).FindStringMatch(s string) (*Match, error) func (*Regexp).FindStringMatchStartingAt(s string, startAt int) (*Match, error) func (*Regexp).FindNextMatch(m *Match) (*Match, error)
MatchEvaluator is a function that takes a match and returns a replacement string to be used func (*Regexp).ReplaceFunc(input string, evaluator MatchEvaluator, startAt, count int) (string, error)
OptimizationOptions controls runtime limits, optional caches, and compile-time fast paths. For MaxBacktrackingStackSize, negative values allow unbounded growth. For replacement data cache size fields, 0 disables persistent retention and -1 means unbounded. For pooled buffer cache size fields, 0 disables pooling and -1 allows all built-in size classes. Defaults are intentionally bounded so Compile is safe for mixed-cardinality inputs. DisableCharClassASCIIBitmap disables compile-time ASCII bitmap construction for character classes. MaxBacktrackingStackSize limits the number of integer slots used by a match's backtracking stack. Negative values disable the limit. MaxCachedReplaceBufferLength limits retained replacement output buffers in the shared size-classed pool. MaxCachedReplacerDataBytes skips caching replacement patterns longer than this many bytes. MaxCachedReplacerDataEntries limits the number of parsed replacement patterns cached per Regexp. MaxCachedRuneBufferLength limits retained string-to-rune buffers in the shared size-classed pool. var DefaultOptimizationOptions
RegexOptions impact the runtime and parsing behavior for each specific regex. They are setable in code as well as in the regex pattern itself. RegexOptions : CompileOption var DefaultUnmarshalOptions const ECMAScript const ExplicitCapture const IgnoreCase const IgnorePatternWhitespace const Multiline const None const RE2 const RightToLeft const Singleline const Unicode
Regexp is the representation of a compiled regular expression. A Regexp is safe for concurrent use by multiple goroutines. A match will time out if it takes (approximately) more than MatchTimeout. This is a safety check in case the match encounters catastrophic backtracking. The default value (DefaultMatchTimeout) causes all time out checking to be suppressed. (*Regexp) Debug() bool FindAllRunesIndex returns a slice of rune index pairs identifying all successive matches in r. FindAllStringIndex returns a slice of byte index pairs identifying all successive matches in s. FindNextMatch returns the next match in the same input string as the match parameter. Will return nil if there is no next match or if given a nil match. FindRunesMatch searches the input rune slice for a Regexp match FindRunesMatchStartingAt searches the input rune slice for a Regexp match starting at the startAt index FindStringMatch searches the input string for a Regexp match FindStringMatchStartingAt searches the input string for a Regexp match starting at the startAt index GetGroupNames Returns the set of strings used to name capturing groups in the expression. GetGroupNumbers returns the integer group numbers corresponding to a group name. GroupNameFromNumber retrieves a group name that corresponds to a group number. It will return "" for an unknown group number. Unnamed groups automatically receive a name that is the decimal string equivalent of its number, except in ECMAScript mode where unnamed groups have no name. GroupNumberFromName returns a group number that corresponds to a group name. Returns -1 if the name is not a recognized group name. Numbered groups automatically get a group name that is the decimal string equivalent of its number, except in ECMAScript mode where unnamed groups have no name. MarshalText implements [encoding.TextMarshaler]. The output matches that of calling the [Regexp.String] method. MatchRunes return true if the runes matches the regex error will be set if a timeout occurs MatchString return true if the string matches the regex error will be set if a timeout occurs Replace searches the input string and replaces each match found with the replacement text. Count will limit the number of matches attempted and startAt will allow us to skip past possible matches at the start of the input (left or right depending on RightToLeft option). Set startAt and count to -1 to go through the whole string ReplaceFunc searches the input string and replaces each match found using the string from the evaluator Count will limit the number of matches attempted and startAt will allow us to skip past possible matches at the start of the input (left or right depending on RightToLeft option). Set startAt and count to -1 to go through the whole string. (*Regexp) RightToLeft() bool Split splits the given input string using the pattern and returns a slice of the parts. Count limits the number of matches to process. If Count is -1, then it will process the input fully. If Count is 0, returns nil. If Count is 1, returns the original input. The only expected error is a Timeout, if it's set. If capturing parentheses are used in the Regex expression, any captured text is included in the resulting string array For example, a pattern of "-" Split("a-b") will return ["a", "b"] but a pattern with "(-)" Split ("a-b") will return ["a", "-", "b"] String returns the source text used to compile the regular expression. UnmarshalText implements [encoding.TextUnmarshaler] by calling [Compile] on the encoded value. *Regexp : encoding.TextMarshaler *Regexp : encoding.TextUnmarshaler *Regexp : expvar.Var *Regexp : fmt.Stringer func Compile(expr string, options ...CompileOption) (*Regexp, error) func MustCompile(str string, options ...CompileOption) *Regexp
Runstackpos int // text to search Runtextend int // current position in text // starting point for search Runtrackpos int Capture captures a subexpression. Note that the capnum used here has already been mapped to a non-sparse index (by the code generator RegexWriter). (*Runner) CheckTimeout() error Get the height of the stack decide whether the pos at the specified index is a boundary or not. It's just not worth emitting inline code for this logic. (*Runner) IsECMABoundary(index int) bool (*Runner) IsMatched(cap int) bool (*Runner) LastIndexOfRune(startIndex int, endIndex int, find rune) int (*Runner) MatchIndex(cap int) int (*Runner) MatchLength(cap int) int StackDepth returns the number of integer slots currently used by the generated engine's backtracking stack. (*Runner) StackPop() int (*Runner) StackPush(val int) (*Runner) StackPush2(val1, val2 int) (*Runner) StackPush3(val1, val2, val3 int) (*Runner) StackPush4(val1, val2, val3, val4 int) (*Runner) StackPush5(val1, val2, val3, val4, val5 int) (*Runner) StackPushN(vals ...int) TryMatchGrapheme consumes one Unicode extended grapheme cluster in the runner's current direction. It is exported for regexp2cg-generated engines. Undo captures until it reaches the specified capture position
// cap group name -> index // size of the capture array // capnum->index // sorted list of capture group names Execute func(*Runner) error // optional bool-only execution with unobservable captures removed // generated candidate search // optional pre-decode candidate search for string input func RegisterEngine(pattern string, engine RuntimeEngineData, options ...CompileOption)
StringPrefixFilter optionally searches string input before the engine decodes it to runes. It returns a byte index for a candidate match start, or ok=false if the regex cannot match. The filter must be conservative: false positives are allowed, false negatives are not.
Package-Level Functions (total 16)
Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against text.
Escape adds backslashes to any special characters in the input string
MustCompile is like Compile but panics if the expression cannot be parsed. It simplifies safe initialization of global variables holding compiled regular expressions.
OptionDebug enables debug output and runner tracing for the compiled regexp.
OptionDisableCharClassASCIIBitmap disables compile-time ASCII bitmaps for character classes.
OptionIsCodeGen enables more expensive compile-time analysis intended for regexp2cg generated engines.
OptionMaintainCaptureOrder assigns named and unnamed capture slots in pattern order.
OptionMaxBacktrackingStackSize limits the number of integer slots used by a match's backtracking stack. Negative values disable the limit. A match that exceeds the limit returns ErrBacktrackingStackLimit.
OptionMaxCachedReplaceBufferLength limits retained replacement output buffers in the shared size-classed pool.
OptionMaxCachedReplacerDataBytes skips caching replacement patterns longer than n bytes.
OptionMaxCachedReplacerDataEntries limits parsed replacement patterns cached per Regexp.
OptionMaxCachedRuneBufferLength limits retained string-to-rune buffers in the shared size-classed pool.
func RegisterEngine(pattern string, engine RuntimeEngineData, options ...CompileOption)
SetTimeoutPeriod is a debug function that sets the frequency of the timeout goroutine's sleep cycle. Defaults to 100ms. The only benefit of setting this lower is that the 1 background goroutine that manages timeouts may exit slightly sooner after all the timeouts have expired. See Github issue #63
StopTimeoutClock should only be used in unit tests to prevent the timeout clock goroutine from appearing like a leaking goroutine
Unescape removes any backslashes from previously-escaped special characters in the input string
Package-Level Variables (total 4)
DefaultMatchTimeout used when running regexp matches -- "forever"
DefaultOptimizationOptions controls the default memory/performance trade-offs used by Compile.
DefaultUnmarshalOptions used when unmarshaling a regex from text
ErrBacktrackingStackLimit is returned when a match exceeds its configured backtracking stack size.
Package-Level Constants (total 11)
const DefaultClockPeriod time.Duration = 100000000
ECMAScript attempts to follow ECMAScript regex behavior rather than C# RegexOptions.ECMAScript compatibility.
const ExplicitCapture RegexOptions = 4 // "n"
const IgnoreCase RegexOptions = 1 // "i"
const Multiline RegexOptions = 2 // "m"
const None RegexOptions = 0
const RE2 RegexOptions = 512 // RE2 (regexp package) compatibility mode
const RightToLeft RegexOptions = 64 // "r"
const Singleline RegexOptions = 16 // "s"
const Unicode RegexOptions = 1024 // "u"