新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFramegregex-方法介紹
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請參考代碼文檔:https://pkg.GO.dev/github.com/gogf/gf/v2/text/gregex

網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為廣告制作等企業(yè)提供專業(yè)服務(wù)。
當(dāng)函數(shù)名中有 ?All ?的時候,它會繼續(xù)查找非重疊的后續(xù)并返回 ?slice ?
當(dāng)函數(shù)名中有 ?String ?的時候,參數(shù)及返回值都是 ?string?,否則為 ?[]byte?
IsMatch/IsMatchString
- 說明: ?
IsMatch()?方法可以測試字符串,判斷是否匹配正則表達式的模式。如果發(fā)現(xiàn)一次匹配,該方法返回?true?,否則返回?false?。 - 格式:
IsMatch(pattern string, src []byte) bool
IsMatchString(pattern string, src string) bool
- Tip: ?
regexp?已經(jīng)在底層處理并緩存了?Regex?對象,以極大地提高執(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/MatchString
- 說明:用來匹配子字符串,?
Match?只返回第一個匹配的結(jié)果。區(qū)別于原生的正則方法,?gregex?是對 ?FindSubmatch?進行封裝,直接返回第一個包括子模式結(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",
// ]
//
}
MatchAll/MatchAllString
- 說明:用來匹配子字符串,?
MatchAll?返回全部的結(jié)果。區(qū)別于原生的正則方法,?gregex?的 ?MatchAll?是對?FindAllSubmatch?方法進行封裝,返回所有結(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",
// ]
//
}
Quote
- 說明:將指定正則表達式中的特定符號進行轉(zhuǎn)義。
- 格式:
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
- 說明:用來替換所有匹配的字符串,返回一個源字符串的拷貝。與?
Replace?方法的區(qū)別在于,該方法可以在閉包中對查詢進行二次判斷或處理,而非簡單的替換。 - 格式:
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?的所有匹配都被應(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!"
//
}
Split
- 說明:將文本內(nèi)容由指定的正則表達式進行切割。不包含元字符,相當(dāng)于?
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",
// ]
}
Validate
- 說明:基于原生方法 ?
compile?封裝,檢查給定的正則表達式是否有效 - 格式:
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",
// }
}
分享題目:創(chuàng)新互聯(lián)GoFrame教程:GoFramegregex-方法介紹
網(wǎng)頁URL:http://fisionsoft.com.cn/article/dhihdcj.html


咨詢
建站咨詢
