Documentation
¶
Overview ¶
Package onigmo is a pure-Go (cgo-free) regular-expression engine compatible with Onigmo — the regular-expression library Ruby uses — that exposes an API shaped like the standard library's regexp package.
Unlike the standard library regexp (and RE2, which it is built on), this engine supports lookahead, lookbehind, backreferences, atomic groups and subexpression calls. Patterns that require those features — which stdlib regexp rejects at Compile time — compile and match here.
The package name is onigmo (not regexp), so it can be imported alongside the standard library regexp without an alias:
import ( "regexp" onigmo "github.com/go-regexp/engine" ) re := onigmo.MustCompile(`v\d+\.\d+\.\d+(?=["<])`) // lookahead: RE2 cannot re.FindAllString(`grab v1.2.3" and v4.5.6< but not v9.9.9`, -1) // -> ["v1.2.3", "v4.5.6"]
A *Regexp is immutable once compiled and safe for concurrent use by multiple goroutines. The heavy matcher state is built lazily on the first match, so a compiled-but-unmatched Regexp pays only the parse cost.
Index ¶
- Constants
- type Encoding
- type Regexp
- func (re *Regexp) Encoding() Encoding
- func (re *Regexp) FindAllString(s string, n int) []string
- func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
- func (re *Regexp) FindString(s string) string
- func (re *Regexp) FindStringIndex(s string) []int
- func (re *Regexp) FindStringSubmatch(s string) []string
- func (re *Regexp) FindStringSubmatchIndex(s string) []int
- func (re *Regexp) FindStringSubmatchIndexAt(s string, pos int) []int
- func (re *Regexp) Match(b []byte) bool
- func (re *Regexp) MatchBounds(s string) (begin, end int, ok bool)
- func (re *Regexp) MatchBoundsAt(s string, pos int) (begin, end int, ok bool)
- func (re *Regexp) MatchString(s string) bool
- func (re *Regexp) NumSubexp() int
- func (re *Regexp) ReplaceAll(src, repl []byte) []byte
- func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
- func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte
- func (re *Regexp) ReplaceAllLiteralString(src, repl string) string
- func (re *Regexp) ReplaceAllString(src, repl string) string
- func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
- func (re *Regexp) String() string
- func (re *Regexp) SubexpIndex(name string) int
- func (re *Regexp) SubexpNames() []string
- func (re *Regexp) Timeout() time.Duration
- func (re *Regexp) WithTimeout(d time.Duration) *Regexp
Constants ¶
const ( // UTF8 is the default encoding: the dot and byte-oriented classes advance by a // whole UTF-8 code point. UTF8 = compile.UTF8 // ASCII8BIT is the binary encoding: every atom advances one byte. ASCII8BIT = compile.ASCII8BIT )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Encoding ¶
Encoding selects how the byte-oriented input-advancing atoms — the dot (`.`) and a byte-oriented character class — traverse the input.
In UTF8 (the default) the dot and byte-oriented classes advance by a whole UTF-8 code point, so `.` matches a complete multi-byte character. In ASCII8BIT (binary mode) every atom advances one byte, and Unicode case-folding (/i) and \p{…} properties operate per byte. Match offsets are byte offsets in both modes.
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
Regexp is a compiled regular expression, safe for concurrent use by multiple goroutines. A Regexp is immutable once compiled; WithTimeout returns a copy carrying a wall-clock match limit rather than mutating the receiver, so a shared Regexp stays concurrency-safe.
func Compile ¶
Compile parses a pattern and returns a compiled Regexp in the default UTF-8 encoding, or an error if the pattern is malformed. Unlike the standard library regexp.Compile, a pattern using lookaround, backreferences, atomic groups or subexpression calls is accepted.
func CompileEnc ¶
CompileEnc is Compile with an explicit input encoding (see Encoding). UTF8 makes the dot and byte-oriented classes advance by a whole code point; ASCII8BIT makes every atom advance one byte.
func MustCompile ¶
MustCompile is like Compile but panics if the pattern cannot be compiled. It simplifies safe initialization of package-level compiled regular expressions.
func MustCompileEnc ¶
MustCompileEnc is like CompileEnc but panics if the pattern cannot be compiled.
func (*Regexp) Encoding ¶
Encoding returns the input encoding the Regexp matches under: UTF8 by default, ASCII8BIT for a binary pattern. It does not trigger the deferred machine build.
func (*Regexp) FindAllString ¶
FindAllString returns a slice of all successive non-overlapping matches of the regular expression in s. A value of n >= 0 limits the result to at most n matches; n < 0 returns all of them. A return value of nil indicates no match. This mirrors regexp.Regexp.FindAllString.
func (*Regexp) FindAllStringIndex ¶
FindAllStringIndex returns a slice of all successive non-overlapping matches of the regular expression in s, expressed as index pairs (see FindStringIndex). The matches are found left to right. A value of n >= 0 limits the result to at most n matches; n < 0 returns all of them. A return value of nil indicates no match. This mirrors regexp.Regexp.FindAllStringIndex.
func (*Regexp) FindString ¶
FindString returns the text of the leftmost match in s of the regular expression. If there is no match, the return value is an empty string, but it will also be empty if the regular expression successfully matches an empty string. Use FindStringIndex if it is necessary to distinguish these cases.
func (*Regexp) FindStringIndex ¶
FindStringIndex returns a two-element slice of integers defining the location of the leftmost match in s of the regular expression. The match itself is at s[loc[0]:loc[1]]. A return value of nil indicates no match. This mirrors regexp.Regexp.FindStringIndex.
func (*Regexp) FindStringSubmatch ¶
FindStringSubmatch returns a slice of strings holding the text of the leftmost match of the regular expression in s and the matches, if any, of its subexpressions. A return value of nil indicates no match. An entry is the empty string when the corresponding subexpression did not participate in the match. This mirrors regexp.Regexp.FindStringSubmatch.
func (*Regexp) FindStringSubmatchIndex ¶
FindStringSubmatchIndex returns a slice holding the index pairs identifying the leftmost match of the regular expression in s and the matches, if any, of its subexpressions, as defined by the 'Submatch' and 'Index' descriptions of regexp.Regexp. A return value of nil indicates no match. This mirrors regexp.Regexp.FindStringSubmatchIndex.
func (*Regexp) FindStringSubmatchIndexAt ¶
FindStringSubmatchIndexAt is FindStringSubmatchIndex for a match anchored exactly at byte offset pos: the whole match must begin at pos (it does not scan forward), with the full string visible so ^, \A and lookbehind see the real prefix s[:pos]. It returns the capture index pairs, or nil if the pattern does not match anchored at pos. pos out of range yields nil.
func (*Regexp) Match ¶
Match reports whether the byte slice b contains any match of the regular expression.
func (*Regexp) MatchBounds ¶
MatchBounds scans s left to right for the leftmost match and returns its whole-match [begin, end) byte span, without extracting submatches. On the lazy-NFA subset the search runs on the linear-time NFA; otherwise it falls to the backtracking VM. The span is identical to FindStringIndex(s).
func (*Regexp) MatchBoundsAt ¶
MatchBoundsAt reports the whole match's [begin, end) byte span for a match anchored exactly at byte offset pos (begin == pos on success), without extracting submatches. Unlike MatchBounds it does not scan forward: it matches at pos or reports ok == false. The whole string stays visible, so ^, \A and lookbehind see the real prefix s[:pos] — the primitive a cursor-anchored tokenizer needs. pos out of range yields ok == false.
func (*Regexp) MatchString ¶
MatchString reports whether the string s contains any match of the regular expression. When the program is in the lazy-NFA subset (no backreference, call, lookaround, atomic group, or over-large bounded loop) the question is answered by the linear-time NFA simulation rather than the backtracking VM.
func (*Regexp) NumSubexp ¶
NumSubexp returns the number of parenthesized capturing subexpressions in the pattern, not counting the whole match (group 0). It matches the semantics of the standard library regexp.Regexp.NumSubexp.
func (*Regexp) ReplaceAll ¶
ReplaceAll is the []byte form of ReplaceAllString: it returns a copy of src with every non-overlapping match replaced by the $-expansion of repl.
func (*Regexp) ReplaceAllFunc ¶
ReplaceAllFunc is the []byte form of ReplaceAllStringFunc.
func (*Regexp) ReplaceAllLiteral ¶
ReplaceAllLiteral is the []byte form of ReplaceAllLiteralString: repl is used literally with no $ expansion.
func (*Regexp) ReplaceAllLiteralString ¶
ReplaceAllLiteralString returns a copy of src, replacing every non-overlapping match of re with repl used literally — no $ expansion is performed. This mirrors regexp.Regexp.ReplaceAllLiteralString.
func (*Regexp) ReplaceAllString ¶
ReplaceAllString returns a copy of src, replacing every non-overlapping match of re with the expansion of repl. Inside repl a $ introduces a submatch reference: $name or ${name} is replaced by the submatch named or numbered name, and $$ is a literal $. A reference to a group that did not participate expands to the empty string. This mirrors regexp.Regexp.ReplaceAllString.
func (*Regexp) ReplaceAllStringFunc ¶
ReplaceAllStringFunc returns a copy of src, replacing every non-overlapping match of re with the return value of repl applied to the matched substring. No $ expansion is performed on repl's result. This mirrors regexp.Regexp.ReplaceAllStringFunc.
func (*Regexp) SubexpIndex ¶
SubexpIndex returns the index of the first subexpression with the given name, or -1 if there is no subexpression with that name. Group 0 (the whole match) has no name. This mirrors regexp.Regexp.SubexpIndex.
func (*Regexp) SubexpNames ¶
SubexpNames returns the names of the parenthesized capturing subexpressions. The name of the first sub-expression is names[1], so that the index into the slice matches the group number used by FindStringSubmatchIndex. Because the whole match has no name, names[0] is always "". A subexpression without a name has an empty string entry. The result is freshly allocated on each call and the caller may modify it. This mirrors regexp.Regexp.SubexpNames.
func (*Regexp) Timeout ¶
Timeout returns the wall-clock limit applied to a single match, or zero if no limit is set.
func (*Regexp) WithTimeout ¶
WithTimeout returns a copy of the Regexp that aborts any single match taking longer than d of wall-clock time, reporting no match. A non-positive d clears the limit. The copy shares the compiled program with the receiver, which is left unchanged, so a Regexp can be shared across goroutines and given per-use timeouts without data races.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
ast
Package ast holds the abstract syntax tree node types for the regular expression grammar.
|
Package ast holds the abstract syntax tree node types for the regular expression grammar. |
|
charset
Package charset classifies a single Unicode code point against the property names this engine recognises for the \p{…} / \P{…} construct.
|
Package charset classifies a single Unicode code point against the property names this engine recognises for the \p{…} / \P{…} construct. |
|
compile
Package compile lowers a syntax AST into the flat instruction program that the backtracking VM executes.
|
Package compile lowers a syntax AST into the flat instruction program that the backtracking VM executes. |
|
syntax
Package syntax holds the scanner and recursive-descent parser that turn an Onigmo/Ruby regular-expression pattern into an abstract syntax tree (the node types live in the sibling ast package).
|
Package syntax holds the scanner and recursive-descent parser that turn an Onigmo/Ruby regular-expression pattern into an abstract syntax tree (the node types live in the sibling ast package). |
|
vm
Package vm executes a compiled program against an input using explicit backtracking with greedy, leftmost-first semantics (as in Ruby/Onigmo).
|
Package vm executes a compiled program against an input using explicit backtracking with greedy, leftmost-first semantics (as in Ruby/Onigmo). |