W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請(qǐng)參考代碼文檔:https://pkg.go.dev/github.com/gogf/gf/v2/text/gregex
當(dāng)函數(shù)名中有 ?All
?的時(shí)候,它會(huì)繼續(xù)查找非重疊的后續(xù)并返回 ?slice
?
當(dāng)函數(shù)名中有 ?String
?的時(shí)候,參數(shù)及返回值都是 ?string
?,否則為 ?[]byte
?
IsMatch()
?方法可以測試字符串,判斷是否匹配正則表達(dá)式的模式。如果發(fā)現(xiàn)一次匹配,該方法返回?true
?,否則返回?false
?。
IsMatch(pattern string, src []byte) bool
IsMatchString(pattern string, src string) bool
regexp
?已經(jīng)在底層處理并緩存了?Regex
?對(duì)象,以極大地提高執(zhí)行效率,無需每次顯式重新創(chuàng)建,下同。
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
?只返回第一個(gè)匹配的結(jié)果。區(qū)別于原生的正則方法,?gregex
?是對(duì) ?FindSubmatch
?進(jìn)行封裝,直接返回第一個(gè)包括子模式結(jié)果的 ?slice
?Match(pattern string, src []byte) ([][]byte, error)
MatchString(pattern string, src string) ([]string, error)
url
?中的參數(shù)。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",
// ]
// <nil>
}
MatchAll
?返回全部的結(jié)果。區(qū)別于原生的正則方法,?gregex
?的 ?MatchAll
?是對(duì)?FindAllSubmatch
?方法進(jìn)行封裝,返回所有結(jié)果集的 ?slice
?,包括結(jié)果集中的子模式的結(jié)果。
MatchAllString(pattern string, src string) ([][]string, error)
MatchAll(pattern string, src []byte) ([][][]byte, error)
url
?中的參數(shù)。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",
// ]
// <nil>
}
Quote(s string) string
func ExampleQuote() {
result := gregex.Quote(`[1-9]\d+`)
g.Dump(result)
// Output:
// "\[1-9\]\\d\+"
}
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:
// <nil>
// "hello gf 2021!"
}
Replace
?方法的區(qū)別在于,該方法可以在閉包中對(duì)查詢進(jìn)行二次判斷或處理,而非簡單的替換。
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!"
// <nil>
}
?ReplaceFuncMatch
?返回?src
?的拷貝,其中?regexp
?的所有匹配都被應(yīng)用于匹配字節(jié)切片的函數(shù)的返回值替換。 返回的替換直接替換。
MatchString
?函數(shù)包含了所有子模式的查詢結(jié)果,而非簡單的替換。
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!"
// <nil>
}
strings.SplitN
?。
Split(pattern string, src string) []string
func ExampleSplit() {
patternStr := `\d+`
str := "hello2020gf"
result := gregex.Split(patternStr, str)
g.Dump(result)
// Output:
// [
// "hello",
// "gf",
// ]
}
compile
?封裝,檢查給定的正則表達(dá)式是否有效
Validate(pattern string) error
func ExampleValidate() {
// Valid match statement
g.Dump(gregex.Validate(`\d+`))
// Mismatched statement
g.Dump(gregex.Validate(`[a-9]\d+`))
// Output:
// <nil>
// {
// Code: "invalid character class range",
// Expr: "a-9",
// }
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: