pcre

package
v0.0.0-...-59443bb Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2015 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

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

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

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
	NO_START_OPTIMIZE = C.PCRE_NO_START_OPTIMIZE
	PARTIAL_HARD      = C.PCRE_PARTIAL_HARD
	PARTIAL_SOFT      = C.PCRE_PARTIAL_SOFT
)

Flags for Match functions

Variables

This section is empty.

Functions

func Compile

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

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

Types

type CompileError

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

func (e *CompileError) String() 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) Group

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

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

func (m *Matcher) Groups() int

Returns the number of groups in the current pattern.

func (*Matcher) Match

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

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

func (m *Matcher) Matches() bool

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

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

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

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

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

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

Switches the matcher object to the specified pattern and subject.

func (*Matcher) ResetString

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

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

type Regexp

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

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

Compile the pattern. If compilation fails, panic.

func (*Regexp) FindIndex

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

func (re Regexp) Groups() int

Returns the number of capture groups in the compiled pattern.

func (Regexp) Matcher

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

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

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

func (Regexp) ReplaceAll

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

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

Jump to

Keyboard shortcuts

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