13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联GoFrame教程:GoFramegregex-方法介绍

以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.GO.dev/github.com/gogf/gf/v2/text/gregex

网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为广告制作等企业提供专业服务。

当函数名中有 ​All ​的时候,它会继续查找非重叠的后续并返回 ​slice

当函数名中有 ​String ​的时候,参数及返回值都是 ​string​,否则为 ​[]byte

IsMatch/IsMatchString

IsMatch(pattern string, src []byte) bool
IsMatchString(pattern string, src string) bool

func ExampleIsMatch() {
	patternStr := `\d+`
	g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello gf!")))
	g.Dump(gregex.IsMatch(patternStr, nil))
	g.Dump(gregex.IsMatch(patternStr, []byte("hello gf!")))

	// Output:
	// true
	// false
	// false
}

Match/MatchString

Match(pattern string, src []byte) ([][]byte, error)
MatchString(pattern string, src string) ([]string, error)

func ExampleMatch() {
	patternStr := `(\w+)=(\w+)`
	matchStr := "https://GoFrame.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
	// This method looks for the first match index
	result, err := gregex.Match(patternStr, []byte(matchStr))
	g.Dump(result)
	g.Dump(err)

	// Output:
	// [
	//     "pageId=1114219",
	//     "pageId",
	//     "1114219",
	// ]
	// 
}

MatchAll/MatchAllString

MatchAllString(pattern string, src string) ([][]string, error)
MatchAll(pattern string, src []byte) ([][][]byte, error)

func ExampleMatchString() {
	patternStr := `(\w+)=(\w+)`
	matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
	// This method looks for the first match index
	result, err := gregex.MatchString(patternStr, matchStr)
	g.Dump(result)
	g.Dump(err)

	// Output:
	// [
	//     "pageId=1114219",
	//     "pageId",
	//     "1114219",
	// ]
	// 
}

Quote

Quote(s string) string

func ExampleQuote() {
	result := gregex.Quote(`[1-9]\d+`)
	g.Dump(result)

	// Output:
	// "\[1-9\]\\d\+"
}

Replace/ReplaceString

Replace(pattern string, replace, src []byte) ([]byte, error)
ReplaceString(pattern, replace, src string) (string, error)

func ExampleReplace() {
	var (
		patternStr  = `\d+`
		str         = "hello gf 2020!"
		repStr      = "2021"
		result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str))
	)
	g.Dump(err)
	g.Dump(result)

	// Output:
	// 
	// "hello gf 2021!"
}

ReplaceFunc/ReplaceStringFunc

ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error)

func ExampleReplaceFuncMatch() {
	var (
		patternStr = `(\d+)~(\d+)`
		str        = "hello gf 2018~2020!"
	)
	// In contrast to [ExampleReplaceFunc]
	// the result contains the `pattern' of all subpatterns that use the matching function
	result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte {
		g.Dump(match)
		match[2] = []byte("2021")
		return bytes.Join(match[1:], []byte("-"))
	})
	g.Dump(result)
	g.Dump(err)

	// Output:
	// [
	//     "2018~2020",
	//     "2018",
	//     "2020",
	// ]
	// "hello gf 2018-2021!"
	// 
}

ReplaceFuncMatch/ReplaceStringFuncMatch

ReplaceFuncMatch​返回​src​的拷贝,其中​regexp​的所有匹配都被应用于匹配字节切片的函数的返回值替换。 返回的替换直接替换。

ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error)

func ExampleReplaceStringFuncMatch() {
	var (
		patternStr = `([A-Z])\w+`
		str        = "hello Golang 2018~2021!"
	)
	// In contrast to [ExampleReplaceFunc]
	// the result contains the `pattern' of all subpatterns that use the matching function
	result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string {
		g.Dump(match)
		match[0] = "Gf"
		return match[0]
	})
	g.Dump(result)
	g.Dump(err)

	// Output:
	// [
	//     "Golang",
	//     "G",
	// ]
	// "hello Gf 2018~2021!"
	// 
}

Split

Split(pattern string, src string) []string

func ExampleSplit() {
	patternStr := `\d+`
	str := "hello2020gf"
	result := gregex.Split(patternStr, str)
	g.Dump(result)

	// Output:
	// [
	//     "hello",
	//     "gf",
	// ]
}

Validate

Validate(pattern string) error

func ExampleValidate() {
	// Valid match statement
	g.Dump(gregex.Validate(`\d+`))
	// Mismatched statement
	g.Dump(gregex.Validate(`[a-9]\d+`))

	// Output:
	// 
	// {
	//     Code: "invalid character class range",
	//     Expr: "a-9",
	// }
}


当前标题:创新互联GoFrame教程:GoFramegregex-方法介绍
分享链接:http://cdbrznjsb.com/article/dhihdcj.html

其他资讯

让你的专属顾问为你服务