Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
基本介绍
gregex
提供了对正则表达式的支持,底层是对标准库regexp
的封装,极大地简化了正则的使用,并采用了解析缓存设计,提高了执行效率。
- 当函数名中有
All
的时候,它会继续查找非重叠的后续并返回slice
- 当函数名中有
String
的时候,参数及返回值都是string
,否则为[]byte
使用方式:
Code Block | ||
---|---|---|
| ||
import "github.com/gogf/gf/v2/text/gregex" |
接口文档:
https://godocpkg.go.orgdev/github.com/gogf/gf/text/gregex
简单示例:
package main
import (
"fmt"
"github.com/gogf/gf/text/gregex"
)
func main() {
match, _ := gregex.MatchString(`(\w+).+\-\-\s*(.+)`, `GF is best! -- John`)
fmt.Printf(`%s says "%s" is the one he loves!`, match[2], match[1])
}
执行后,输出结果为:
John says "GF" is the one he loves!
IsMatch / IsMatchString
IsMatch()
方法可以测试字符串,判断是否匹配正则表达式的模式。如果发现一次匹配,该方法返回"true",否则返回"false"。格式:
Code Block |
---|
IsMatch(pattern string, src []byte) bool
IsMatchString(pattern string, src string) bool |
regexp
已经在底层处理并缓存了Regex对象,无需每次显式重新创建,下同。示例
Code Block |
---|
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
只返回第一个匹配的结果。区别于原生的正则方法,gregex
是对FindSubmatch
进行封装,直接返回第一个包括子模式结果的 slice格式
Code Block |
---|
Match(pattern string, src []byte) ([][]byte, error)
MatchString(pattern string, src string) ([]string, error) |
示例:匹配 url 中的参数。
Code Block |
---|
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 / MatchAllString
MatchAll
返回全部的结果。区别于原生的正则方法,gregex
的 MatchAll 是对FindAllSubmatch
方法进行封装,返回所有结果集的 slice,包括结果集中的子模式的结果。格式:
Code Block |
---|
MatchAllString(pattern string, src string) ([][]string, error)
MatchAll(pattern string, src []byte) ([][][]byte, error) |
相关文档
Children Display all true excerptType simple
示例:下面这个例子是匹配 url 中的参数。
Code Block |
---|
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
格式:
Code Block |
---|
Quote(s string) string |
示例:
Code Block |
---|
func ExampleQuote() {
result := gregex.Quote(`[1-9]\d+`)
g.Dump(result)
// Output:
// "\[1-9\]\\d\+"
} |
Replace / ReplaceString
格式:
Code Block |
---|
Replace(pattern string, replace, src []byte) ([]byte, error)
ReplaceString(pattern, replace, src string) (string, error) |
示例
Code Block |
---|
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!"
} |
ReplaceFunc / ReplaceStringFunc
格式:
Code Block |
---|
ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error) |
示例:
Code Block |
---|
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 / ReplaceStringFuncMatch
replaceFuncMatch
返回src的拷贝,其中regexp的所有匹配都被应用于匹配字节切片的函数的返回值替换。 返回的替换直接替换。
matchString
函数包含了所有子模式的查询结果,而非简单的替换。格式:
Code Block |
---|
ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error) |
示例:
Code Block |
---|
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>
} |
Split
格式:
Code Block |
---|
Split(pattern string, src string) []string |
示例:
Code Block |
---|
func ExampleSplit() {
patternStr := `\d+`
str := "hello2020gf"
result := gregex.Split(patternStr, str)
g.Dump(result)
// Output:
// [
// "hello",
// "gf",
// ]
} |
Validate
compile
封装,检查给定的正则表达式是否有效格式:
Code Block |
---|
Validate(pattern string) error |
示例:
Panel | ||
---|---|---|
| ||
|