regexp 正则
最后更新于:2022-04-02 02:45:42
[TOC]
## 语法
```
type Regexp
func Compile(expr string) (*Regexp, error)
func CompilePOSIX(expr string) (*Regexp, error)
// MustCompile类似Compile但会在解析失败时panic
func MustCompile(str string) *Regexp
func MustCompilePOSIX(str string) *Regexp
func (re *Regexp) String() string
func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
func (re *Regexp) Longest()
func (re *Regexp) NumSubexp() int
func (re *Regexp) SubexpNames() []string
func (re *Regexp) Match(b []byte) bool
func (re *Regexp) MatchString(s string) bool
func (re *Regexp) MatchReader(r io.RuneReader) bool
func (re *Regexp) Find(b []byte) []byte
func (re *Regexp) FindString(s string) string
func (re *Regexp) FindIndex(b []byte) (loc []int)
func (re *Regexp) FindStringIndex(s string) (loc []int)
func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)
func (re *Regexp) FindSubmatch(b []byte) [][]byte
func (re *Regexp) FindStringSubmatch(s string) []string
func (re *Regexp) FindSubmatchIndex(b []byte) []int
func (re *Regexp) FindStringSubmatchIndex(s string) []int
func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int
func (re *Regexp) FindAll(b []byte, n int) [][]byte
func (re *Regexp) FindAllString(s string, n int) []string
func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte
func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string
func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int
func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
func (re *Regexp) Split(s string, n int) []string
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) ReplaceAllLiteral(src, repl []byte) []byte
func (re *Regexp) ReplaceAllLiteralString(src, repl string) string
func (re *Regexp) ReplaceAll(src, repl []byte) []byte
func (re *Regexp) ReplaceAllString(src, repl string) string
func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
```
## 实例
### 判断正则是否符合要求
方法一:
```
bool, err := regexp.MatchString("p([a-z]+)ch", "peach")
```
方法二:
```
r, _ := regexp.Compile("p([a-z]+)ch")
bool:=r.MatchString("peach")
```
### Find / FindAll
```
re := regexp.MustCompile(`foo.?`)
fmt.Printf("%q\n", re.FindStringSubmatch(`seafood fool`)) // ["food"]
re1 := regexp.MustCompile(`foo.?`)
fmt.Printf("%q\n", re1.FindAllString(`seafood fool`, -1)) // ["food" "fool"]
```
### FindSubmatch / FindAllSubmatch 获取子查询
```
re := regexp.MustCompile(`foo(.?)`)
fmt.Printf("%q\n", re.FindSubmatch([]byte(`seafood fool`))) // ["food" "d"]
re1 := regexp.MustCompile(`foo(.?)`)
fmt.Printf("%q\n", re1.FindAllSubmatch([]byte(`seafood fool`), -1)) // [["food" "d"] ["fool" "l"]]
```
### Index
```
re := regexp.MustCompile(`ab?`)
fmt.Println(re.FindStringIndex("tablett")) // [1 3]
fmt.Println(re.FindStringIndex("foo") == nil) // true
fmt.Println(re.FindAllStringIndex("tablettab1", -1)) // [[1 3] [7 9]]
fmt.Println(re.FindAllStringIndex("foo", -1) == nil) // true
```
### Replace
```
re := regexp.MustCompile(`a(bc)`)
fmt.Printf("%+v\n", re.ReplaceAllString("abcdefabcdef", "=${1}=")) // =bc=def=bc=def
fmt.Printf("%+v\n", re.ReplaceAllLiteralString("abcdefabcdef", "=${1}=")) // =${1}=def=${1}=def
fmt.Printf("%+v\n", re.ReplaceAllStringFunc("abcdefabcdef", func(s string) string {
//s -> abc
return "=" + s + "="
})) // =abc=def=abc=def
```
### Split
```
zp := regexp.MustCompile(`z+`)
fmt.Println(zp.Split("pizza", -1))
fmt.Println(zp.Split("pizza", 0))
fmt.Println(zp.Split("pizza", 1))
fmt.Println(zp.Split("pizza", 2))
// Output:
// [pi a]
// []
// [pizza]
// [pi a]
```
';