gregex

gregex提供了对正则表达式的支持,底层是对标准库regexp的封装,极大地简化了正则的使用,并采用了解析缓存设计,提高了执行效率。

使用方式:

  1. import "gitee.com/johng/gf/g/util/gregex"

方法列表: godoc.org/github.com/johng-cn/gf/g/util/gregex

  1. func IsMatch(pattern string, src []byte) bool
  2. func IsMatchString(pattern string, src string) bool
  3. func MatchAllString(pattern string, src string) ([][]string, error)
  4. func MatchString(pattern string, src string) ([]string, error)
  5. func Quote(s string) string
  6. func Replace(pattern string, replace, src []byte) ([]byte, error)
  7. func ReplaceFunc(pattern string, src []byte, repl func(b []byte) []byte) ([]byte, error)
  8. func ReplaceString(pattern, replace, src string) (string, error)
  9. func ReplaceStringFunc(pattern string, src string, repl func(s string) string) (string, error)
  10. func Validate(pattern string) error

示例1,基本使用

  1. package main
  2. import (
  3. "fmt"
  4. "gitee.com/johng/gf/g/util/gregex"
  5. )
  6. func main() {
  7. match, _ := gregex.MatchString(`(\w+).+\-\-\s*(.+)`, `GF is best! -- John`)
  8. fmt.Printf(`%s says "%s" is the one he loves!`, match[2], match[1])
  9. }

执行后,输出结果为:

  1. John says "GF" is the one he loves!