regex

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ANCHORED          = C.PCRE_ANCHORED
	BSR_ANYCRLF       = C.PCRE_BSR_ANYCRLF
	BSR_UNICODE       = C.PCRE_BSR_UNICODE
	NEWLINE_ANY       = C.PCRE_NEWLINE_ANY
	NEWLINE_ANYCRLF   = C.PCRE_NEWLINE_ANYCRLF
	NEWLINE_CR        = C.PCRE_NEWLINE_CR
	NEWLINE_CRLF      = C.PCRE_NEWLINE_CRLF
	NEWLINE_LF        = C.PCRE_NEWLINE_LF
	NO_START_OPTIMIZE = C.PCRE_NO_START_OPTIMIZE
	NO_UTF8_CHECK     = C.PCRE_NO_UTF8_CHECK
)

Flags for Compile and Match functions.

View Source
const (
	CASELESS          = C.PCRE_CASELESS
	DOLLAR_ENDONLY    = C.PCRE_DOLLAR_ENDONLY
	DOTALL            = C.PCRE_DOTALL
	DUPNAMES          = C.PCRE_DUPNAMES
	EXTENDED          = C.PCRE_EXTENDED
	EXTRA             = C.PCRE_EXTRA
	FIRSTLINE         = C.PCRE_FIRSTLINE
	JAVASCRIPT_COMPAT = C.PCRE_JAVASCRIPT_COMPAT
	MULTILINE         = C.PCRE_MULTILINE
	NEVER_UTF         = C.PCRE_NEVER_UTF
	NO_AUTO_CAPTURE   = C.PCRE_NO_AUTO_CAPTURE
	UNGREEDY          = C.PCRE_UNGREEDY
	UTF8              = C.PCRE_UTF8
	UCP               = C.PCRE_UCP
)

Flags for Compile functions

View Source
const (
	NOTBOL           = C.PCRE_NOTBOL
	NOTEOL           = C.PCRE_NOTEOL
	NOTEMPTY         = C.PCRE_NOTEMPTY
	NOTEMPTY_ATSTART = C.PCRE_NOTEMPTY_ATSTART
	PARTIAL_HARD     = C.PCRE_PARTIAL_HARD
	PARTIAL_SOFT     = C.PCRE_PARTIAL_SOFT
)

Flags for Match functions

View Source
const (
	STUDY_JIT_COMPILE              = C.PCRE_STUDY_JIT_COMPILE
	STUDY_JIT_PARTIAL_SOFT_COMPILE = C.PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
	STUDY_JIT_PARTIAL_HARD_COMPILE = C.PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
)

Flags for Study function

View Source
const (
	ERROR_NOMATCH        = C.PCRE_ERROR_NOMATCH
	ERROR_NULL           = C.PCRE_ERROR_NULL
	ERROR_BADOPTION      = C.PCRE_ERROR_BADOPTION
	ERROR_BADMAGIC       = C.PCRE_ERROR_BADMAGIC
	ERROR_UNKNOWN_OPCODE = C.PCRE_ERROR_UNKNOWN_OPCODE
	ERROR_UNKNOWN_NODE   = C.PCRE_ERROR_UNKNOWN_NODE
	ERROR_NOMEMORY       = C.PCRE_ERROR_NOMEMORY
	ERROR_NOSUBSTRING    = C.PCRE_ERROR_NOSUBSTRING
	ERROR_MATCHLIMIT     = C.PCRE_ERROR_MATCHLIMIT
	ERROR_CALLOUT        = C.PCRE_ERROR_CALLOUT
	ERROR_BADUTF8        = C.PCRE_ERROR_BADUTF8
	ERROR_BADUTF8_OFFSET = C.PCRE_ERROR_BADUTF8_OFFSET
	ERROR_PARTIAL        = C.PCRE_ERROR_PARTIAL
	ERROR_BADPARTIAL     = C.PCRE_ERROR_BADPARTIAL
	ERROR_RECURSIONLIMIT = C.PCRE_ERROR_RECURSIONLIMIT
	ERROR_INTERNAL       = C.PCRE_ERROR_INTERNAL
	ERROR_BADCOUNT       = C.PCRE_ERROR_BADCOUNT
	ERROR_JIT_STACKLIMIT = C.PCRE_ERROR_JIT_STACKLIMIT
)

Exec-time and get/set-time error codes

Variables

This section is empty.

Functions

This section is empty.

Types

type CompileError

type CompileError struct {
	Pattern string // The failed pattern
	Message string // The error message
	Offset  int    // Byte position of error
}

CompileError holds details about a compilation error, as returned by the Compile function. The offset is the byte position in the pattern string at which the error was detected.

func (*CompileError) Error

func (e *CompileError) Error() string

Error converts a compile error to a string

type Matcher

type Matcher struct {
	// contains filtered or unexported fields
}

Matcher objects provide a place for storing match results. They can be created by the Matcher and MatcherString functions, or they can be initialized with Reset or ResetString.

func (*Matcher) Exec

func (m *Matcher) Exec(subject []byte, flags int) int

Exec tries to match the specified byte slice to the current pattern. Returns the raw pcre_exec error code.

func (*Matcher) ExecString

func (m *Matcher) ExecString(subject string, flags int) int

ExecString tries to match the specified subject string to the current pattern. It returns the raw pcre_exec error code.

func (*Matcher) Extract

func (m *Matcher) Extract() [][]byte

Extract returns a slice of byte slices for a single match. The first byte slice contains the complete match. Subsequent byte slices contain the captured groups. If there was no match then nil is returned.

func (*Matcher) ExtractString

func (m *Matcher) ExtractString() []string

ExtractString returns a slice of strings for a single match. The first string contains the complete match. Subsequent strings in the slice contain the captured groups. If there was no match then nil is returned.

func (*Matcher) Group

func (m *Matcher) Group(group int) []byte

Group returns the numbered capture group of the last match (performed by Matcher, MatcherString, Reset, ResetString, Match, or MatchString). Group 0 is the part of the subject which matches the whole pattern; the first actual capture group is numbered 1. Capture groups which are not present return a nil slice.

func (*Matcher) GroupIndices

func (m *Matcher) GroupIndices(group int) []int

GroupIndices returns the numbered capture group positions of the last match (performed by Matcher, MatcherString, Reset, ResetString, Match, or MatchString). Group 0 is the part of the subject which matches the whole pattern; the first actual capture group is numbered 1. Capture groups which are not present return a nil slice.

func (*Matcher) GroupString

func (m *Matcher) GroupString(group int) string

GroupString returns the numbered capture group as a string. Group 0 is the part of the subject which matches the whole pattern; the first actual capture group is numbered 1. Capture groups which are not present return an empty string.

func (*Matcher) Groups

func (m *Matcher) Groups() int

Groups returns the number of groups in the current pattern.

func (*Matcher) Index

func (m *Matcher) Index() (loc []int)

Index returns the start and end of the first match, if a previous call to Matcher, MatcherString, Reset, ResetString, Match or MatchString succeeded. loc[0] is the start and loc[1] is the end.

func (*Matcher) Init

func (m *Matcher) Init(re *Regexp)

Init binds an existing Matcher object to the given Regexp.

func (*Matcher) Match

func (m *Matcher) Match(subject []byte, flags int) bool

Match tries to match the specified byte slice to the current pattern by calling Exec and collects the result. Returns true if the match succeeds.

func (*Matcher) MatchString

func (m *Matcher) MatchString(subject string, flags int) bool

MatchString tries to match the specified subject string to the current pattern by calling ExecString and collects the result. Returns true if the match succeeds.

func (*Matcher) Matches

func (m *Matcher) Matches() bool

Matches returns true if a previous call to Matcher, MatcherString, Reset, ResetString, Match or MatchString succeeded.

func (*Matcher) Named

func (m *Matcher) Named(group string) ([]byte, error)

Named returns the value of the named capture group. This is a nil slice if the capture group is not present. If the name does not refer to a group then error is non-nil.

func (*Matcher) NamedPresent

func (m *Matcher) NamedPresent(group string) (bool, error)

NamedPresent returns true if the named capture group is present. If the name does not refer to a group then error is non-nil.

func (*Matcher) NamedString

func (m *Matcher) NamedString(group string) (string, error)

NamedString returns the value of the named capture group, or an empty string if the capture group is not present. If the name does not refer to a group then error is non-nil.

func (*Matcher) Partial

func (m *Matcher) Partial() bool

Partial returns true if a previous call to Matcher, MatcherString, Reset, ResetString, Match or MatchString found a partial match.

func (*Matcher) Present

func (m *Matcher) Present(group int) bool

Present returns true if the numbered capture group is present in the last match (performed by Matcher, MatcherString, Reset, ResetString, Match, or MatchString). Group numbers start at 1. A capture group can be present and match the empty string.

func (*Matcher) Reset

func (m *Matcher) Reset(re Regexp, subject []byte, flags int) bool

Reset switches the matcher object to the specified regexp and subject. It also starts a first match on subject.

func (*Matcher) ResetString

func (m *Matcher) ResetString(re Regexp, subject string, flags int) bool

ResetString switches the matcher object to the given regexp and subject. It also starts a first match on subject.

type Regexp

type Regexp struct {
	// contains filtered or unexported fields
}

Regexp holds a reference to a compiled regular expression. Use Compile or MustCompile to create such objects.

func Compile

func Compile(pattern string, flags int) (Regexp, error)

Compile the pattern and return a compiled regexp. If compilation fails, the second return value holds a *CompileError.

func CompileJIT

func CompileJIT(pattern string, comFlags, jitFlags int) (Regexp, error)

CompileJIT is a combination of Compile and Study. It first compiles the pattern and if this succeeds calls Study on the compiled pattern. comFlags are Compile flags, jitFlags are study flags. If compilation fails, the second return value holds a *CompileError.

func MustCompile

func MustCompile(pattern string, flags int) (re Regexp)

MustCompile compiles the pattern. If compilation fails, panic.

func MustCompileJIT

func MustCompileJIT(pattern string, comFlags, jitFlags int) (re Regexp)

MustCompileJIT compiles and studies the pattern. On failure it panics.

func (*Regexp) FindIndex

func (re *Regexp) FindIndex(bytes []byte, flags int) (loc []int)

FindIndex returns the start and end of the first match, or nil if no match. loc[0] is the start and loc[1] is the end.

func (Regexp) Groups

func (re Regexp) Groups() int

Groups returns the number of capture groups in the compiled pattern.

func (Regexp) Matcher

func (re Regexp) Matcher(subject []byte, flags int) (m *Matcher)

Matcher creates a new matcher object, with the byte slice as subject. It also starts a first match on subject. Test for success with Matches().

func (Regexp) MatcherString

func (re Regexp) MatcherString(subject string, flags int) (m *Matcher)

MatcherString creates a new matcher, with the specified subject string. It also starts a first match on subject. Test for success with Matches().

func (Regexp) NewMatcher

func (re Regexp) NewMatcher() (m *Matcher)

NewMatcher creates a new matcher object for the given Regexp.

func (Regexp) ReplaceAll

func (re Regexp) ReplaceAll(bytes, repl []byte, flags int) []byte

ReplaceAll returns a copy of a byte slice where all pattern matches are replaced by repl.

func (Regexp) ReplaceAllString

func (re Regexp) ReplaceAllString(in, repl string, flags int) string

ReplaceAllString is equivalent to ReplaceAll with string return type.

func (*Regexp) Study

func (re *Regexp) Study(flags int) error

Study adds Just-In-Time compilation to a Regexp. This may give a huge speed boost when matching. If an error occurs, return value is non-nil. Flags optionally specifies JIT compilation options for partial matches.

Jump to

Keyboard shortcuts

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