...

Package regexp2

import "github.com/dlclark/regexp2"
Overview
Index
Subdirectories

Overview ▾

Package regexp2 is a regexp package that has an interface similar to Go's framework regexp engine but uses a more feature full regex engine behind the scenes.

It doesn't have constant time guarantees, but it allows backtracking and is compatible with Perl5 and .NET. You'll likely be better off with the RE2 engine from the regexp package and should only use this if you need to write very complex patterns or require compatibility with .NET.

Index ▾

Constants
Variables
func Escape(input string) string
func SetTimeoutCheckPeriod(d time.Duration)
func StopTimeoutClock()
func Unescape(input string) (string, error)
type Capture
    func (c *Capture) Runes() []rune
    func (c *Capture) String() string
type Group
type Match
    func (m *Match) GroupByName(name string) *Group
    func (m *Match) GroupByNumber(num int) *Group
    func (m *Match) GroupCount() int
    func (m *Match) Groups() []Group
type MatchEvaluator
type RegexOptions
type Regexp
    func Compile(expr string, opt RegexOptions) (*Regexp, error)
    func MustCompile(str string, opt RegexOptions) *Regexp
    func (re *Regexp) Debug() bool
    func (re *Regexp) FindNextMatch(m *Match) (*Match, error)
    func (re *Regexp) FindRunesMatch(r []rune) (*Match, error)
    func (re *Regexp) FindRunesMatchStartingAt(r []rune, startAt int) (*Match, error)
    func (re *Regexp) FindStringMatch(s string) (*Match, error)
    func (re *Regexp) FindStringMatchStartingAt(s string, startAt int) (*Match, error)
    func (re *Regexp) GetGroupNames() []string
    func (re *Regexp) GetGroupNumbers() []int
    func (re *Regexp) GroupNameFromNumber(i int) string
    func (re *Regexp) GroupNumberFromName(name string) int
    func (re *Regexp) MatchRunes(r []rune) (bool, error)
    func (re *Regexp) MatchString(s string) (bool, error)
    func (re *Regexp) Replace(input, replacement string, startAt, count int) (string, error)
    func (re *Regexp) ReplaceFunc(input string, evaluator MatchEvaluator, startAt, count int) (string, error)
    func (re *Regexp) RightToLeft() bool
    func (re *Regexp) String() string

Package files

fastclock.go match.go regexp.go replace.go runner.go

Constants

const (
    None                    RegexOptions = 0x0
    IgnoreCase                           = 0x0001 // "i"
    Multiline                            = 0x0002 // "m"
    ExplicitCapture                      = 0x0004 // "n"
    Compiled                             = 0x0008 // "c"
    Singleline                           = 0x0010 // "s"
    IgnorePatternWhitespace              = 0x0020 // "x"
    RightToLeft                          = 0x0040 // "r"
    Debug                                = 0x0080 // "d"
    ECMAScript                           = 0x0100 // "e"
    RE2                                  = 0x0200 // RE2 (regexp package) compatibility mode
    Unicode                              = 0x0400 // "u"
)
const DefaultClockPeriod = 100 * time.Millisecond

Variables

Default timeout used when running regexp matches -- "forever"

var DefaultMatchTimeout = time.Duration(math.MaxInt64)

func Escape

func Escape(input string) string

Escape adds backslashes to any special characters in the input string

func SetTimeoutCheckPeriod

func SetTimeoutCheckPeriod(d time.Duration)

SetTimeoutPeriod is a debug function that sets the frequency of the timeout goroutine's sleep cycle. Defaults to 100ms. The only benefit of setting this lower is that the 1 background goroutine that manages timeouts may exit slightly sooner after all the timeouts have expired. See Github issue #63

func StopTimeoutClock

func StopTimeoutClock()

StopTimeoutClock should only be used in unit tests to prevent the timeout clock goroutine from appearing like a leaking goroutine

func Unescape

func Unescape(input string) (string, error)

Unescape removes any backslashes from previously-escaped special characters in the input string

type Capture

Capture is a single capture of text within the larger original string

type Capture struct {

    // the position in the original string where the first character of
    // captured substring was found.
    Index int
    // the length of the captured substring.
    Length int
    // contains filtered or unexported fields
}

func (*Capture) Runes

func (c *Capture) Runes() []rune

Runes returns the captured text as a rune slice

func (*Capture) String

func (c *Capture) String() string

String returns the captured text as a String

type Group

Group is an explicit or implit (group 0) matched group within the pattern

type Group struct {
    Capture // the last capture of this group is embeded for ease of use

    Name     string    // group name
    Captures []Capture // captures of this group
}

type Match

Match is a single regex result match that contains groups and repeated captures

	-Groups
   -Capture
type Match struct {
    Group //embeded group 0
    // contains filtered or unexported fields
}

func (*Match) GroupByName

func (m *Match) GroupByName(name string) *Group

GroupByName returns a group based on the name of the group, or nil if the group name does not exist

func (*Match) GroupByNumber

func (m *Match) GroupByNumber(num int) *Group

GroupByNumber returns a group based on the number of the group, or nil if the group number does not exist

func (*Match) GroupCount

func (m *Match) GroupCount() int

GroupCount returns the number of groups this match has matched

func (*Match) Groups

func (m *Match) Groups() []Group

Groups returns all the capture groups, starting with group 0 (the full match)

type MatchEvaluator

MatchEvaluator is a function that takes a match and returns a replacement string to be used

type MatchEvaluator func(Match) string

type RegexOptions

RegexOptions impact the runtime and parsing behavior for each specific regex. They are setable in code as well as in the regex pattern itself.

type RegexOptions int32

type Regexp

Regexp is the representation of a compiled regular expression. A Regexp is safe for concurrent use by multiple goroutines.

type Regexp struct {
    // A match will time out if it takes (approximately) more than
    // MatchTimeout. This is a safety check in case the match
    // encounters catastrophic backtracking.  The default value
    // (DefaultMatchTimeout) causes all time out checking to be
    // suppressed.
    MatchTimeout time.Duration
    // contains filtered or unexported fields
}

func Compile

func Compile(expr string, opt RegexOptions) (*Regexp, 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, opt RegexOptions) *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) Debug

func (re *Regexp) Debug() bool

func (*Regexp) FindNextMatch

func (re *Regexp) FindNextMatch(m *Match) (*Match, error)

FindNextMatch returns the next match in the same input string as the match parameter. Will return nil if there is no next match or if given a nil match.

func (*Regexp) FindRunesMatch

func (re *Regexp) FindRunesMatch(r []rune) (*Match, error)

FindRunesMatch searches the input rune slice for a Regexp match

func (*Regexp) FindRunesMatchStartingAt

func (re *Regexp) FindRunesMatchStartingAt(r []rune, startAt int) (*Match, error)

FindRunesMatchStartingAt searches the input rune slice for a Regexp match starting at the startAt index

func (*Regexp) FindStringMatch

func (re *Regexp) FindStringMatch(s string) (*Match, error)

FindStringMatch searches the input string for a Regexp match

func (*Regexp) FindStringMatchStartingAt

func (re *Regexp) FindStringMatchStartingAt(s string, startAt int) (*Match, error)

FindStringMatchStartingAt searches the input string for a Regexp match starting at the startAt index

func (*Regexp) GetGroupNames

func (re *Regexp) GetGroupNames() []string

GetGroupNames Returns the set of strings used to name capturing groups in the expression.

func (*Regexp) GetGroupNumbers

func (re *Regexp) GetGroupNumbers() []int

GetGroupNumbers returns the integer group numbers corresponding to a group name.

func (*Regexp) GroupNameFromNumber

func (re *Regexp) GroupNameFromNumber(i int) string

GroupNameFromNumber retrieves a group name that corresponds to a group number. It will return "" for and unknown group number. Unnamed groups automatically receive a name that is the decimal string equivalent of its number.

func (*Regexp) GroupNumberFromName

func (re *Regexp) GroupNumberFromName(name string) int

GroupNumberFromName returns a group number that corresponds to a group name. Returns -1 if the name is not a recognized group name. Numbered groups automatically get a group name that is the decimal string equivalent of its number.

func (*Regexp) MatchRunes

func (re *Regexp) MatchRunes(r []rune) (bool, error)

MatchRunes return true if the runes matches the regex error will be set if a timeout occurs

func (*Regexp) MatchString

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

MatchString return true if the string matches the regex error will be set if a timeout occurs

func (*Regexp) Replace

func (re *Regexp) Replace(input, replacement string, startAt, count int) (string, error)

Replace searches the input string and replaces each match found with the replacement text. Count will limit the number of matches attempted and startAt will allow us to skip past possible matches at the start of the input (left or right depending on RightToLeft option). Set startAt and count to -1 to go through the whole string

func (*Regexp) ReplaceFunc

func (re *Regexp) ReplaceFunc(input string, evaluator MatchEvaluator, startAt, count int) (string, error)

ReplaceFunc searches the input string and replaces each match found using the string from the evaluator Count will limit the number of matches attempted and startAt will allow us to skip past possible matches at the start of the input (left or right depending on RightToLeft option). Set startAt and count to -1 to go through the whole string.

func (*Regexp) RightToLeft

func (re *Regexp) RightToLeft() bool

func (*Regexp) String

func (re *Regexp) String() string

String returns the source text used to compile the regular expression.

Subdirectories

Name Synopsis
..
syntax