Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"strings"
"github.com/kamichidu/go-regexp-re"
)
func main() {
pattern := `a(b+)c`
src := "abbc"
repl := "X"
// Package-level functions
{
matched, _ := regexp.Match(pattern, []byte(src))
fmt.Printf("Match: %v\n", matched)
matchedString, _ := regexp.MatchString(pattern, src)
fmt.Printf("MatchString: %v\n", matchedString)
matchedReader, _ := regexp.MatchReader(pattern, strings.NewReader(src))
fmt.Printf("MatchReader: %v\n", matchedReader)
re, _ := regexp.Compile(pattern)
fmt.Printf("Compile: %v\n", re.String())
reMust := regexp.MustCompile(pattern)
fmt.Printf("MustCompile: %v\n", reMust.String())
quoted := regexp.QuoteMeta(`[a-z]`)
fmt.Printf("QuoteMeta: %s\n", quoted)
}
re := regexp.MustCompile(pattern)
// Regexp methods
{
fmt.Printf("String: %s\n", re.String())
fmt.Printf("NumSubexp: %d\n", re.NumSubexp())
fmt.Printf("SubexpNames: %q\n", re.SubexpNames())
fmt.Printf("SubexpIndex: %d\n", re.SubexpIndex("foo"))
prefix, complete := re.LiteralPrefix()
fmt.Printf("LiteralPrefix: %q, %v\n", prefix, complete)
fmt.Printf("Match: %v\n", re.Match([]byte(src)))
fmt.Printf("MatchString: %v\n", re.MatchString(src))
fmt.Printf("MatchReader: %v\n", re.MatchReader(strings.NewReader(src)))
fmt.Printf("Find: %q\n", re.Find([]byte(src)))
fmt.Printf("FindIndex: %v\n", re.FindIndex([]byte(src)))
fmt.Printf("FindString: %q\n", re.FindString(src))
fmt.Printf("FindStringIndex: %v\n", re.FindStringIndex(src))
fmt.Printf("FindReaderIndex: %v\n", re.FindReaderIndex(strings.NewReader(src)))
fmt.Printf("FindSubmatch: %q\n", re.FindSubmatch([]byte(src)))
fmt.Printf("FindSubmatchIndex: %v\n", re.FindSubmatchIndex([]byte(src)))
fmt.Printf("FindStringSubmatch: %q\n", re.FindStringSubmatch(src))
fmt.Printf("FindStringSubmatchIndex: %v\n", re.FindStringSubmatchIndex(src))
fmt.Printf("FindReaderSubmatchIndex: %v\n", re.FindReaderSubmatchIndex(strings.NewReader(src)))
fmt.Printf("FindAll: %q\n", re.FindAll([]byte(src), -1))
fmt.Printf("FindAllIndex: %v\n", re.FindAllIndex([]byte(src), -1))
fmt.Printf("FindAllString: %q\n", re.FindAllString(src, -1))
fmt.Printf("FindAllStringIndex: %v\n", re.FindAllStringIndex(src, -1))
fmt.Printf("FindAllSubmatch: %q\n", re.FindAllSubmatch([]byte(src), -1))
fmt.Printf("FindAllSubmatchIndex: %v\n", re.FindAllSubmatchIndex([]byte(src), -1))
fmt.Printf("FindAllStringSubmatch: %q\n", re.FindAllStringSubmatch(src, -1))
fmt.Printf("FindAllStringSubmatchIndex: %v\n", re.FindAllStringSubmatchIndex(src, -1))
fmt.Printf("ReplaceAll: %q\n", re.ReplaceAll([]byte(src), []byte(repl)))
fmt.Printf("ReplaceAllString: %q\n", re.ReplaceAllString(src, repl))
fmt.Printf("ReplaceAllLiteral: %q\n", re.ReplaceAllLiteral([]byte(src), []byte(repl)))
fmt.Printf("ReplaceAllLiteralString: %q\n", re.ReplaceAllLiteralString(src, repl))
fmt.Printf("ReplaceAllFunc: %q\n", re.ReplaceAllFunc([]byte(src), func(b []byte) []byte { return b }))
fmt.Printf("ReplaceAllStringFunc: %q\n", re.ReplaceAllStringFunc(src, func(s string) string { return s }))
fmt.Printf("Split: %q\n", re.Split(src, -1))
marshaled, _ := re.MarshalText()
fmt.Printf("MarshalText: %s\n", marshaled)
var re2 regexp.Regexp
_ = re2.UnmarshalText(marshaled)
fmt.Printf("UnmarshalText: %s\n", re2.String())
dst := []byte("Initial: ")
template := "Captured: $1"
match := re.FindSubmatchIndex([]byte(src))
fmt.Printf("Expand: %q\n", re.Expand(dst, []byte(template), []byte(src), match))
fmt.Printf("ExpandString: %q\n", re.ExpandString(dst, template, src, match))
reCopy := re.Copy()
fmt.Printf("Copy: %v\n", reCopy.String())
}
// Methods specifically known to be excluded from the "Core API Subset"
// These will fail compilation if uncommented.
{
// re.Longest()
// regexp.CompilePOSIX(pattern)
// regexp.MustCompilePOSIX(pattern)
}
}
Output: Match: true MatchString: true MatchReader: true Compile: a(b+)c MustCompile: a(b+)c QuoteMeta: \[a-z\] String: a(b+)c NumSubexp: 1 SubexpNames: ["" ""] SubexpIndex: -1 LiteralPrefix: "a", false Match: true MatchString: true MatchReader: true Find: "abbc" FindIndex: [0 4] FindString: "abbc" FindStringIndex: [0 4] FindReaderIndex: [0 4] FindSubmatch: ["abbc" "bb"] FindSubmatchIndex: [0 4 1 3] FindStringSubmatch: ["abbc" "bb"] FindStringSubmatchIndex: [0 4 1 3] FindReaderSubmatchIndex: [0 4 1 3] FindAll: ["abbc"] FindAllIndex: [[0 4]] FindAllString: ["abbc"] FindAllStringIndex: [[0 4]] FindAllSubmatch: [["abbc" "bb"]] FindAllSubmatchIndex: [[0 4 1 3]] FindAllStringSubmatch: [["abbc" "bb"]] FindAllStringSubmatchIndex: [[0 4 1 3]] ReplaceAll: "X" ReplaceAllString: "X" ReplaceAllLiteral: "X" ReplaceAllLiteralString: "X" ReplaceAllFunc: "abbc" ReplaceAllStringFunc: "abbc" Split: ["" ""] MarshalText: a(b+)c UnmarshalText: a(b+)c Expand: "Initial: Captured: bb" ExpandString: "Initial: Captured: bb" Copy: a(b+)c
Index ¶
- func Match(pattern string, b []byte) (matched bool, err error)
- func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)deprecated
- func MatchString(pattern string, s string) (matched bool, err error)
- func QuoteMeta(s string) string
- type CompileOptions
- type ExplainOptions
- type Regexp
- func Compile(expr string) (*Regexp, error)
- func CompileContext(ctx context.Context, expr string) (*Regexp, error)
- func CompileContextWithOptions(ctx context.Context, expr string, opts CompileOptions) (*Regexp, error)
- func CompileWithOptions(expr string, opt CompileOptions) (*Regexp, error)
- func MustCompile(expr string) *Regexp
- func (re *Regexp) Copy() *Regexp
- func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte
- func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte
- func (re *Regexp) Explain() string
- func (re *Regexp) ExplainWithOptions(opts ExplainOptions) string
- func (re *Regexp) Find(b []byte) []byte
- func (re *Regexp) FindAll(b []byte, n int) [][]byte
- func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
- func (re *Regexp) FindAllString(s string, n int) []string
- func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
- func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string
- func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
- func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte
- func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int
- func (re *Regexp) FindIndex(b []byte) []int
- func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)deprecated
- func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []intdeprecated
- func (re *Regexp) FindString(s string) string
- func (re *Regexp) FindStringIndex(s string) []int
- func (re *Regexp) FindStringSubmatch(s string) []string
- func (re *Regexp) FindStringSubmatchIndex(s string) []int
- func (re *Regexp) FindSubmatch(b []byte) [][]byte
- func (re *Regexp) FindSubmatchIndex(b []byte) []int
- func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
- func (re *Regexp) MarshalText() ([]byte, error)
- func (re *Regexp) Match(b []byte) bool
- func (re *Regexp) MatchReader(r io.RuneReader) booldeprecated
- func (re *Regexp) MatchString(s string) bool
- func (re *Regexp) NumSubexp() int
- func (re *Regexp) ReplaceAll(src, repl []byte) []byte
- func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
- func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte
- func (re *Regexp) ReplaceAllLiteralString(src, repl string) string
- func (re *Regexp) ReplaceAllString(src, repl string) string
- func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
- func (re *Regexp) Split(s string, n int) []string
- func (re *Regexp) String() string
- func (re *Regexp) SubexpIndex(name string) int
- func (re *Regexp) SubexpNames() []string
- func (re *Regexp) UnmarshalText(text []byte) error
- type UnsupportedError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatchReader
deprecated
func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)
MatchReader reports whether the regular expression pattern matches the text read by the RuneReader.
Deprecated: This function performs a full memory load of the reader's content before processing and is not truly streaming.
Types ¶
type CompileOptions ¶
type CompileOptions struct {
MaxMemory int
// contains filtered or unexported fields
}
type ExplainOptions ¶
type ExplainOptions struct {
// MaxPatternLength defines the maximum number of characters to display for the pattern.
// Use -1 for unlimited length. 0 defaults to 80.
MaxPatternLength int
}
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
func CompileWithOptions ¶
func CompileWithOptions(expr string, opt CompileOptions) (*Regexp, error)
func MustCompile ¶
func (*Regexp) ExpandString ¶
func (*Regexp) ExplainWithOptions ¶
func (re *Regexp) ExplainWithOptions(opts ExplainOptions) string
func (*Regexp) FindAllStringSubmatch ¶
func (*Regexp) FindAllStringSubmatchIndex ¶
func (*Regexp) FindAllSubmatchIndex ¶
func (*Regexp) FindReaderIndex
deprecated
func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)
FindReaderIndex returns a two-element slice of integers defining the location of the leftmost match of the regular expression in text read from the RuneReader.
Deprecated: This method performs a full memory load of the reader's content before processing and is not truly streaming.
func (*Regexp) FindReaderSubmatchIndex
deprecated
func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int
FindReaderSubmatchIndex returns a slice holding the index pairs identifying the leftmost match of the regular expression of the text read from the RuneReader, and the matches, if any, of its capturing groups.
Deprecated: This method performs a full memory load of the reader's content before processing and is not truly streaming.
func (*Regexp) FindString ¶
func (*Regexp) FindStringIndex ¶
func (*Regexp) FindStringSubmatch ¶
func (*Regexp) FindStringSubmatchIndex ¶
func (*Regexp) FindSubmatch ¶
func (*Regexp) FindSubmatchIndex ¶
func (*Regexp) LiteralPrefix ¶
func (*Regexp) MarshalText ¶
func (*Regexp) MatchReader
deprecated
func (re *Regexp) MatchReader(r io.RuneReader) bool
MatchReader reports whether the regular expression matches the text read by the RuneReader.
Deprecated: This method performs a full memory load of the reader's content before processing and is not truly streaming.
func (*Regexp) MatchString ¶
func (*Regexp) ReplaceAll ¶
func (*Regexp) ReplaceAllFunc ¶
func (*Regexp) ReplaceAllLiteral ¶
func (*Regexp) ReplaceAllLiteralString ¶
func (*Regexp) ReplaceAllString ¶
func (*Regexp) ReplaceAllStringFunc ¶
func (*Regexp) SubexpIndex ¶
func (*Regexp) SubexpNames ¶
func (*Regexp) UnmarshalText ¶
type UnsupportedError ¶
type UnsupportedError = syntax.UnsupportedError
UnsupportedError represents a regular expression pattern that is not supported by the current DFA-based engine.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
regexp-re-explain
command
|
|
|
internal
|
|
|
tools/bench-compare
command
|
|
|
tools/history-gen
command
|
|
|
tools/landscape-gen
command
|
|