golang-pkg-pcre: github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre Index | Files

package pcre

import "github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre"

This package provides access to the Perl Compatible Regular Expresion library, PCRE.

It implements two main types, Regexp and Matcher. Regexp objects store a compiled regular expression. They are immutable. Compilation of regular expressions using Compile or MustCompile is slightly expensive, so these objects should be kept and reused, instead of compiling them from scratch for each matching attempt.

Matcher objects keeps the results of a match against a []byte or string subject. The Group and GroupString functions provide access to capture groups; both versions work no matter if the subject was a []byte or string, but the version with the matching type is slightly more efficient.

Matcher objects contain some temporary space and refer the original subject. They are mutable and can be reused (using Match, MatchString, Reset or ResetString).

For details on the regular expression language implemented by this package and the flags defined below, see the PCRE documentation.

Index

Package Files

pcre.go

Constants

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_UTF8_CHECK   = C.PCRE_NO_UTF8_CHECK
)

Flags for Compile and Match functions.

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
    NO_AUTO_CAPTURE   = C.PCRE_NO_AUTO_CAPTURE
    UNGREEDY          = C.PCRE_UNGREEDY
    UTF8              = C.PCRE_UTF8
)

Flags for Compile functions

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

Flags for Match functions

func Compile Uses

func Compile(pattern string, flags int) (Regexp, *CompileError)

Try to compile the pattern. If an error occurs, the second return value is non-nil.

type CompileError Uses

type CompileError struct {
    Pattern string
    Message string
    Offset  int
}

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) String Uses

func (e *CompileError) String() string

type Matcher Uses

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) Group Uses

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

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) GroupString Uses

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

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 Uses

func (m *Matcher) Groups() int

Returns the number of groups in the current pattern.

func (*Matcher) Match Uses

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

Tries to match the speficied byte array slice to the current pattern. Returns true if the match succeeds.

func (*Matcher) MatchString Uses

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

Tries to match the speficied subject string to the current pattern. Returns true if the match succeeds.

func (*Matcher) Matches Uses

func (m *Matcher) Matches() bool

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

func (*Matcher) Named Uses

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

Returns the value of the named capture group. This is a nil slice if the capture group is not present. Panics if the name does not refer to a group.

func (*Matcher) NamedPresent Uses

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

Returns true if the named capture group is present. Panics if the name does not refer to a group.

func (*Matcher) NamedString Uses

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

Returns the value of the named capture group, or an empty string if the capture group is not present. Panics if the name does not refer to a group.

func (*Matcher) Present Uses

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

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 Uses

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

Switches the matcher object to the specified pattern and subject.

func (*Matcher) ResetString Uses

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

Switches the matcher object to the specified pattern and subject string.

type Regexp Uses

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

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

func MustCompile Uses

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

Compile the pattern. If compilation fails, panic.

func (*Regexp) FindIndex Uses

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

Return 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 Uses

func (re Regexp) Groups() int

Returns the number of capture groups in the compiled pattern.

func (Regexp) Matcher Uses

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

Returns a new matcher object, with the byte array slice as a subject.

func (Regexp) MatcherString Uses

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

Returns a new matcher object, with the specified subject string.

func (Regexp) ReplaceAll Uses

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

Return a copy of a byte slice with pattern matches replaced by repl.

Package pcre imports 3 packages (graph) and is imported by 13 packages. Updated 2020-08-09. Refresh now. Tools for package owners.