Go Home Page
The Go Programming Language

Package regexp

import "regexp"

Package regexp implements a simple regular expression library.

The syntax of the regular expressions accepted is:

regexp:
	concatenation { '|' concatenation }
concatenation:
	{ closure }
closure:
	term [ '*' | '+' | '?' ]
term:
	'^'
	'$'
	'.'
	character
	'[' [ '^' ] character-ranges ']'
	'(' regexp ')'

Package files

regexp.go

Variables

Error codes returned by failures to parse an expression.

var (
    ErrInternal            = os.NewError("internal error")
    ErrUnmatchedLpar       = os.NewError("unmatched '('")
    ErrUnmatchedRpar       = os.NewError("unmatched ')'")
    ErrUnmatchedLbkt       = os.NewError("unmatched '['")
    ErrUnmatchedRbkt       = os.NewError("unmatched ']'")
    ErrBadRange            = os.NewError("bad range in character class")
    ErrExtraneousBackslash = os.NewError("extraneous backslash")
    ErrBadClosure          = os.NewError("repeated closure (**, ++, etc.)")
    ErrBareClosure         = os.NewError("closure applies to nothing")
    ErrBadBackslash        = os.NewError("illegal backslash escape")
)

func Match

func Match(pattern string, b []byte) (matched bool, error os.Error)

Match checks whether a textual regular expression matches a byte slice. More complicated queries need to use Compile and the full Regexp interface.

func MatchString

func MatchString(pattern string, s string) (matched bool, error os.Error)

MatchString checks whether a textual regular expression matches a string. More complicated queries need to use Compile and the full Regexp interface.

func QuoteMeta

func QuoteMeta(s string) string

QuoteMeta returns a string that quotes all regular expression metacharacters inside the argument text; the returned string is a regular expression matching the literal text. For example, QuoteMeta(`[foo]`) returns `\[foo\]`.

type Regexp

Regexp is the representation of a compiled regular expression. The public interface is entirely through methods.

type Regexp struct {
    // contains unexported fields
}

func Compile

func Compile(str string) (regexp *Regexp, error os.Error)

Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against text.

func MustCompile

func MustCompile(str string) *Regexp

MustCompile is like Compile but panics if the expression cannot be parsed. It simplifies safe initialization of global variables holding compiled regular expressions.

func (*Regexp) AllMatches

func (re *Regexp) AllMatches(b []byte, n int) [][]byte

AllMatches slices the byte slice b into substrings that are successive matches of the Regexp within b. If n > 0, the function returns at most n matches. Text that does not match the expression will be skipped. Empty matches abutting a preceding match are ignored. The function returns a slice containing the matching substrings.

func (*Regexp) AllMatchesIter

func (re *Regexp) AllMatchesIter(b []byte, n int) <-chan []byte

AllMatchesIter slices the byte slice b into substrings that are successive matches of the Regexp within b. If n > 0, the function returns at most n matches. Text that does not match the expression will be skipped. Empty matches abutting a preceding match are ignored. The function returns a channel that iterates over the matching substrings.

func (*Regexp) AllMatchesString

func (re *Regexp) AllMatchesString(s string, n int) []string

AllMatchesString slices the string s into substrings that are successive matches of the Regexp within s. If n > 0, the function returns at most n matches. Text that does not match the expression will be skipped. Empty matches abutting a preceding match are ignored. The function returns a slice containing the matching substrings.

func (*Regexp) AllMatchesStringIter

func (re *Regexp) AllMatchesStringIter(s string, n int) <-chan string

AllMatchesStringIter slices the string s into substrings that are successive matches of the Regexp within s. If n > 0, the function returns at most n matches. Text that does not match the expression will be skipped. Empty matches abutting a preceding match are ignored. The function returns a channel that iterates over the matching substrings.

func (*Regexp) Execute

func (re *Regexp) Execute(b []byte) (a []int)

Execute matches the Regexp against the byte slice b. The return value is an array of integers, in pairs, identifying the positions of subslices matched by the expression.

b[a[0]:a[1]] is the subslice matched by the entire expression.
b[a[2*i]:a[2*i+1]] for i > 0 is the subslice matched by the ith parenthesized subexpression.

A negative value means the subexpression did not match any element of the slice. An empty array means "no match".

func (*Regexp) ExecuteString

func (re *Regexp) ExecuteString(s string) (a []int)

ExecuteString matches the Regexp against the string s. The return value is an array of integers, in pairs, identifying the positions of substrings matched by the expression.

s[a[0]:a[1]] is the substring matched by the entire expression.
s[a[2*i]:a[2*i+1]] for i > 0 is the substring matched by the ith parenthesized subexpression.

A negative value means the subexpression did not match any element of the string. An empty array means "no match".

func (*Regexp) Match

func (re *Regexp) Match(b []byte) bool

Match returns whether the Regexp matches the byte slice b. The return value is a boolean: true for match, false for no match.

func (*Regexp) MatchSlices

func (re *Regexp) MatchSlices(b []byte) (a [][]byte)

MatchSlices matches the Regexp against the byte slice b. The return value is an array of subslices matched by the expression.

a[0] is the subslice matched by the entire expression.
a[i] for i > 0 is the subslice matched by the ith parenthesized subexpression.

An empty array means “no match”.

func (*Regexp) MatchString

func (re *Regexp) MatchString(s string) bool

MatchString returns whether the Regexp matches the string s. The return value is a boolean: true for match, false for no match.

func (*Regexp) MatchStrings

func (re *Regexp) MatchStrings(s string) (a []string)

MatchStrings matches the Regexp against the string s. The return value is an array of strings matched by the expression.

a[0] is the substring matched by the entire expression.
a[i] for i > 0 is the substring matched by the ith parenthesized subexpression.

An empty array means “no match”.

func (*Regexp) NumSubexp

func (re *Regexp) NumSubexp() int

NumSubexp returns the number of parenthesized subexpressions in this Regexp.

func (*Regexp) ReplaceAll

func (re *Regexp) ReplaceAll(src, repl []byte) []byte

ReplaceAll returns a copy of src in which all matches for the Regexp have been replaced by repl. No support is provided for expressions (e.g. \1 or $1) in the replacement text.

func (*Regexp) ReplaceAllString

func (re *Regexp) ReplaceAllString(src, repl string) string

ReplaceAllString returns a copy of src in which all matches for the Regexp have been replaced by repl. No support is provided for expressions (e.g. \1 or $1) in the replacement string.