go語言提供豐富的正則函數以覆蓋各類平常正則需求。
與go語言標準庫風格同樣,該標準庫先定義了一個結構體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) 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函數
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) boolspa
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) stringcode
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() []stringregexp
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)
}
複製代碼
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)
}
複製代碼