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 CalloutBlock
- type CalloutFlags
- 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) SetCallout(fn func(cb *CalloutBlock) int32) error
- 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) MatchUnsetBackref = 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
const ( CalloutStartMatch = CalloutFlags(lib.DPCRE2_CALLOUT_STARTMATCH) CalloutBacktrack = CalloutFlags(lib.DPCRE2_CALLOUT_BACKTRACK) )
const Unset = math.MaxUint
Variables ¶
This section is empty.
Functions ¶
func ConvertGlob ¶
ConvertGlob converts the given glob into a pcre regular expression, and then returns the result.
Types ¶
type CalloutBlock ¶
type CalloutBlock struct { // Version contains the version number of the block format. // The current version is 2. Version uint32 // CalloutNumber contains the number of the callout, in the range 0-255. // This is the number that follows "?C". For callouts with string arguments, // this will always be zero. CalloutNumber uint32 // CaptureTop contains the number of the highest numbered substring // captured so far plus one. If no substrings have yet been captured, // CaptureTop will be set to 1. CaptureTop uint32 // CaptureLast contains the number of the last substring that was captured. CaptureLast uint32 // Substrings contains all of the substrings captured so far. Substrings []string Mark string // Subject contains the string passed to the match function. Subject string // StartMatch contains the offset within the subject at which the current match attempt started. StartMatch uint // CurrentPosition contains the offset of the current match pointer within the subject. CurrentPosition uint // PatternPosition contains the offset within the pattern string to the next item to be matched. PatternPosition uint // NextItemLength contains the length of the next item to be processed in the pattern string. NextItemLength uint // CalloutStringOffset contains the code unit offset to the start of the callout argument string within the original pattern string. CalloutStringOffset uint // CalloutString is the string for the callout. For numerical callouts, this will always be empty. CalloutString string // CalloutFlags contains the following flags: // CalloutStartMatch // This is set for the first callout after the start of matching for each new starting position in the subject. // CalloutBacktrack // This is set if there has been a matching backtrack since the previous callout, or since the start of matching if this is the first callout from a pcre2_match() run. // // Both bits are set when a backtrack has caused a "bumpalong" to a new starting position in the subject. CalloutFlags CalloutFlags }
CalloutBlock contains the data passed to callout functions
type CalloutFlags ¶
type CalloutFlags uint32
type CompileOption ¶
type CompileOption uint32
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) 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) 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) SetCallout ¶
func (r *Regexp) SetCallout(fn func(cb *CalloutBlock) int32) error
SetCallout sets a callout function that will be called at specified points in the matching operation. fn should return zero if it ran successfully or a non-zero integer to force an error. See https://www.pcre.org/current/doc/html/pcre2callout.html for more information.
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) SubexpIndex ¶
SubexpIndex returns the index of the subexpression with the given name, or -1 if there is no subexpression with that name.