go语言标准库 - regexp

1,795 阅读2分钟

一、目录

二、regexp

1. 简介

go语言提供丰富的正则函数以覆盖各种日常正则需求。
与go语言标准库风格一样,该标准库先定义了一个结构体Regexp,然后在这个结构体上挂载功能函数。
最后提供初始化函数,并封装几个简单的函数(标准实现),方便我们进行基础使用。

1.1. Regexp初始化函数

  1. func Compile(expr string) (*Regexp, error)
    以正则表达式为基础返回Regexp实体。
  2. func CompilePOSIX(expr string) (*Regexp, error)
    以正则表达式为基础返回Regexp实体。但是正则表达式需要符合POSIX ERE (egrep) 语法。
  3. func MustCompile(str string) *Regexp
    如果不能解析正则表达式则直接触发panic。
  4. func MustCompilePOSIX(str string) *Regexp
    正则表达式需要符合POSIX ERE (egrep) 语法。如果不能解析正则表达式则直接触发panic。

1.2. 标准实现

  1. func Match(pattern string, b []byte) (matched bool, err error)
    判断字节切片b,是否匹配正则pattern
  2. func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)
    正则匹配io.RuneReader
  3. func MatchString(pattern string, s string) (matched bool, err error)
    正则匹配字符串
  4. func QuoteMeta(s string) string
    将正则表达式元字符转义并返回。

1.3. Regexp挂载方法

1. 正则查询

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) 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) (loc []int)
func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)
func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int
func (re *Regexp) FindString(s string) string
func (re *Regexp) FindStringIndex(s string) (loc []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

2. 正则匹配

func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
func (re *Regexp) Match(b []byte) bool
func (re *Regexp) MatchReader(r io.RuneReader) bool
func (re *Regexp) MatchString(s string) bool

3. 正则替换

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

4. 其他

func (re *Regexp) Copy() *Regexp
func (re *Regexp) Longest()
func (re *Regexp) NumSubexp() int
func (re *Regexp) Split(s string, n int) []string
func (re *Regexp) String() string
func (re *Regexp) SubexpNames() []string

2. 使用标准实现

package main

import (
	"fmt"
	"regexp"
)

func main() {
	matched, err := regexp.Match(`foo*`, []byte(`food`))
	fmt.Println(matched, err)
	matched, err = regexp.Match(`foo*`, []byte(`water`))
	fmt.Println(matched, err)
}

3. 使用挂载函数

package main

import (
	"fmt"
	"regexp"
)

func main() {
	findRes := regexp.MustCompile(`foo*`).FindAllString("food fool foot foolish", -1)
	fmt.Println(findRes)
	// replaceRes := regexp.MustCompile(`foo\s+$`).ReplaceAllString("food fool foot foolish", "xxx")
	// fmt.Println(replaceRes)
}