Documentation
¶
Overview ¶
Package pcre is a library that provides pcre2 regular expressions in pure Go, allowing for features such as cross-compiling.
The lib directory contains source code automatically translated from pcre2's C source code for each supported architecture and/or OS. This package wraps the automatically-translated source to provide a safe interface as close to Go's regexp library as possible.
Index ¶
- Constants
- func ConvertGlob(glob string) (string, error)
- func Glob(glob string) ([]string, error)
- func Version() string
- type CompileOption
- type PcreError
- type Regexp
- func (r *Regexp) Close() error
- func (r *Regexp) Find(b []byte) []byte
- func (r *Regexp) FindAll(b []byte, n int) [][]byte
- func (r *Regexp) FindAllIndex(b []byte, n int) [][]int
- func (r *Regexp) FindAllString(s string, n int) []string
- func (r *Regexp) FindAllStringIndex(s string, n int) [][]int
- func (r *Regexp) FindAllStringSubmatch(s string, n int) [][]string
- func (r *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
- func (r *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte
- func (r *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int
- func (r *Regexp) FindIndex(b []byte) []int
- func (r *Regexp) FindString(s string) string
- func (r *Regexp) FindStringIndex(s string) []int
- func (r *Regexp) FindStringSubmatch(s string) []string
- func (r *Regexp) FindStringSubmatchIndex(s string) []int
- func (r *Regexp) FindSubmatch(b []byte) [][]byte
- func (r *Regexp) FindSubmatchIndex(b []byte) []int
- func (r *Regexp) Match(b []byte) bool
- func (r *Regexp) MatchString(s string) bool
- func (r *Regexp) NumSubexp() int
- func (r *Regexp) ReplaceAll(src, repl []byte) []byte
- func (r *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
- func (r *Regexp) ReplaceAllLiteral(src, repl []byte) []byte
- func (r *Regexp) ReplaceAllLiteralString(src, repl string) string
- func (r *Regexp) ReplaceAllString(src, repl string) string
- func (r *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
- func (r *Regexp) Split(s string, n int) []string
- func (r *Regexp) String() string
- func (r *Regexp) SubexpIndex(name string) int
Constants ¶
const ( Anchored = CompileOption(lib.DPCRE2_ANCHORED) AllowEmptyClass = CompileOption(lib.DPCRE2_ALLOW_EMPTY_CLASS) AltBsux = CompileOption(lib.DPCRE2_ALT_BSUX) AltCircumflex = CompileOption(lib.DPCRE2_ALT_CIRCUMFLEX) AltVerbnames = CompileOption(lib.DPCRE2_ALT_VERBNAMES) AutoCallout = CompileOption(lib.DPCRE2_AUTO_CALLOUT) Caseless = CompileOption(lib.DPCRE2_CASELESS) DollarEndOnly = CompileOption(lib.DPCRE2_DOLLAR_ENDONLY) DotAll = CompileOption(lib.DPCRE2_DOTALL) DupNames = CompileOption(lib.DPCRE2_DUPNAMES) EndAnchored = CompileOption(lib.DPCRE2_ENDANCHORED) Extended = CompileOption(lib.DPCRE2_EXTENDED) FirstLine = CompileOption(lib.DPCRE2_FIRSTLINE) Literal = CompileOption(lib.DPCRE2_LITERAL) MatchInvalidUTF = CompileOption(lib.DPCRE2_MATCH_INVALID_UTF) MactchUnsetBackref = CompileOption(lib.DPCRE2_MATCH_UNSET_BACKREF) Multiline = CompileOption(lib.DPCRE2_MULTILINE) NeverBackslashC = CompileOption(lib.DPCRE2_NEVER_BACKSLASH_C) NeverUCP = CompileOption(lib.DPCRE2_NEVER_UCP) NeverUTF = CompileOption(lib.DPCRE2_NEVER_UTF) NoAutoCapture = CompileOption(lib.DPCRE2_NO_AUTO_CAPTURE) NoAutoPossess = CompileOption(lib.DPCRE2_NO_AUTO_POSSESS) NoDotStarAnchor = CompileOption(lib.DPCRE2_NO_DOTSTAR_ANCHOR) NoStartOptimize = CompileOption(lib.DPCRE2_NO_START_OPTIMIZE) NoUTFCheck = CompileOption(lib.DPCRE2_NO_UTF_CHECK) UCP = CompileOption(lib.DPCRE2_UCP) Ungreedy = CompileOption(lib.DPCRE2_UNGREEDY) UseOffsetLimit = CompileOption(lib.DPCRE2_USE_OFFSET_LIMIT) UTF = CompileOption(lib.DPCRE2_UTF) )
Compile option bits
Variables ¶
This section is empty.
Functions ¶
func ConvertGlob ¶
ConvertGlob converts the given glob into a pcre regular expression, and then returns the result.
func Glob ¶
Glob returns a list of matches for the given glob pattern. It returns nil if there was no match. If the glob contains "**", it will recurse through the directory, which may be extremely slow depending on which directory is being searched.
Types ¶
type PcreError ¶
type PcreError struct {
// contains filtered or unexported fields
}
PcreError represents errors returned by underlying pcre2 functions.
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
Regexp represents a pcre2 regular expression
func Compile ¶
Compile runs CompileOpts with no options.
Close() should be called on the returned expression once it is no longer needed.
func CompileGlob ¶
CompileGlob is a convenience function that converts a glob to a pcre regular expression and then compiles it.
func CompileOpts ¶
func CompileOpts(pattern string, options CompileOption) (*Regexp, error)
CompileOpts compiles the provided pattern using the given options.
Close() should be called on the returned expression once it is no longer needed.
func MustCompile ¶
MustCompile compiles the given pattern and panics if there was an error
Close() should be called on the returned expression once it is no longer needed.
func MustCompileOpts ¶
func MustCompileOpts(pattern string, options CompileOption) *Regexp
MustCompileOpts compiles the given pattern with the given options and panics if there was an error.
Close() should be called on the returned expression once it is no longer needed.
func (*Regexp) Close ¶
Close frees resources used by the regular expression.
func (*Regexp) Find ¶
Find returns the leftmost match of the regular expression. A return value of nil indicates no match.
func (*Regexp) FindAll ¶
FindAll returns all matches of the regular expression. A return value of nil indicates no match.
func (*Regexp) FindAllIndex ¶
FindAll returns indices of all matches of the regular expression. A return value of nil indicates no match.
func (*Regexp) FindAllString ¶
FinAllString is the String version of FindAll
func (*Regexp) FindAllStringIndex ¶
FindAllStringIndex is the String version of FindIndex
func (*Regexp) FindAllStringSubmatch ¶
FindAllStringSubmatch is the String version of FindAllSubmatch
func (*Regexp) FindAllStringSubmatchIndex ¶
FindAllStringSubmatchIndex is the String version of FindAllSubmatchIndex
func (*Regexp) FindAllSubmatch ¶
FindAllSubmatch returns a slice of all matches and submatches of the regular expression. It will return no more than n matches. If n < 0, it will return all matches.
func (*Regexp) FindAllSubmatchIndex ¶
FindAllSubmatch returns a slice of all indeces representing the locations of matches and submatches, if any, of the regular expression. It will return no more than n matches. If n < 0, it will return all matches.
func (*Regexp) FindIndex ¶
FindIndex returns a two-element slice of integers representing the location of the leftmost match of the regular expression.
func (*Regexp) FindString ¶
FindString is the String version of Find
func (*Regexp) FindStringIndex ¶
FindStringIndex is the String version of FindIndex
func (*Regexp) FindStringSubmatch ¶
FindStringSubmatch is the string version of FindSubmatch
func (*Regexp) FindStringSubmatchIndex ¶
FindStringSubmatchIndex is the String version of FindSubmatchIndex
func (*Regexp) FindSubmatch ¶
FindSubmatch returns a slice containing the match as the first element, and the submatches as the subsequent elements.
func (*Regexp) FindSubmatchIndex ¶
FindSubmatchIndex returns a slice of index pairs representing the match and submatches, if any.
func (*Regexp) Match ¶
Match reports whether b contains a match of the regular expression
func (*Regexp) MatchString ¶
MatchString is the String version of Match
func (*Regexp) NumSubexp ¶
NumSubexp returns the number of parenthesized subexpressions in the regular expression.
func (*Regexp) ReplaceAll ¶
ReplaceAll returns a copy of src, replacing matches of the regular expression with the replacement text repl. Inside repl, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch and $name would represent the text of the subexpression called "name".
func (*Regexp) ReplaceAllFunc ¶
ReplaceAllFunc returns a copy of src in which all matches of the regular expression have been replaced by the return value of function repl applied to the matched byte slice. The replacement returned by repl is substituted directly, without using Expand.
func (*Regexp) ReplaceAllLiteral ¶
ReplaceAllLiteral returns a copy of src, replacing matches of the regular expression with the replacement bytes repl. The replacement is substituted directly, without using Expand.
func (*Regexp) ReplaceAllLiteralString ¶
ReplaceAllLiteralString is the String version of ReplaceAllLiteral
func (*Regexp) ReplaceAllString ¶
ReplaceAllString is the String version of ReplaceAll
func (*Regexp) ReplaceAllStringFunc ¶
ReplaceAllStringFunc is the String version of ReplaceAllFunc
func (*Regexp) Split ¶
Split slices s into substrings separated by the expression and returns a slice of the substrings between those expression matches.
Example:
s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5) // s: ["", "b", "b", "c", "cadaaae"]
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
func (*Regexp) String ¶
String returns the text of the regular expression used for compilation.