Files
gf/text/gregex/gregex_z_example_test.go

280 lines
5.8 KiB
Go
Raw Permalink Normal View History

2021-11-01 12:17:53 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gregex_test
2021-11-02 20:08:15 +08:00
import (
2021-11-05 23:56:08 +08:00
"bytes"
2021-11-16 21:40:04 +08:00
"fmt"
2021-11-13 23:23:55 +08:00
"strings"
2021-11-03 11:22:17 +08:00
"github.com/gogf/gf/v2/frame/g"
2021-11-02 20:08:15 +08:00
"github.com/gogf/gf/v2/text/gregex"
)
2021-11-01 12:17:53 +08:00
func ExampleIsMatch() {
2021-11-06 00:49:15 +08:00
patternStr := `\d+`
2021-11-04 21:39:28 +08:00
g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello gf!")))
2021-11-03 11:28:54 +08:00
g.Dump(gregex.IsMatch(patternStr, nil))
2021-11-04 21:39:28 +08:00
g.Dump(gregex.IsMatch(patternStr, []byte("hello gf!")))
2021-11-01 12:17:53 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-02 20:08:15 +08:00
// true
2021-11-03 11:28:54 +08:00
// false
// false
2021-11-01 12:17:53 +08:00
}
func ExampleIsMatchString() {
2021-11-06 00:49:15 +08:00
patternStr := `\d+`
2021-11-04 21:39:28 +08:00
g.Dump(gregex.IsMatchString(patternStr, "hello 2022! hello gf!"))
g.Dump(gregex.IsMatchString(patternStr, "hello gf!"))
2021-11-03 11:28:54 +08:00
g.Dump(gregex.IsMatchString(patternStr, ""))
2021-11-01 12:17:53 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-02 20:08:15 +08:00
// true
2021-11-03 11:28:54 +08:00
// false
// false
2021-11-01 12:17:53 +08:00
}
func ExampleMatch() {
2021-11-05 23:56:08 +08:00
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))
2021-11-03 11:28:54 +08:00
g.Dump(result)
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:16:51 +08:00
// [
2021-11-06 01:47:00 +08:00
// "pageId=1114219",
// "pageId",
// "1114219",
2021-11-06 01:16:51 +08:00
// ]
2021-11-04 21:39:28 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
2021-11-05 23:56:08 +08:00
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)
2021-11-03 11:28:54 +08:00
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:16:51 +08:00
// [
2021-11-06 01:47:00 +08:00
// "pageId=1114219",
// "pageId",
// "1114219",
2021-11-06 01:16:51 +08:00
// ]
2021-11-04 21:39:28 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
2021-11-05 23:56:08 +08:00
func ExampleMatchAll() {
patternStr := `(\w+)=(\w+)`
matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
result, err := gregex.MatchAll(patternStr, []byte(matchStr))
g.Dump(result)
2021-11-03 11:28:54 +08:00
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:47:00 +08:00
// [
// [
// "pageId=1114219",
// "pageId",
// "1114219",
// ],
// [
// "searchId=8QC5D1D2E",
// "searchId",
// "8QC5D1D2E",
// ],
2021-11-06 01:16:51 +08:00
// ]
2021-11-04 21:39:28 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
2021-11-05 23:56:08 +08:00
func ExampleMatchAllString() {
patternStr := `(\w+)=(\w+)`
matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
result, err := gregex.MatchAllString(patternStr, matchStr)
g.Dump(result)
2021-11-03 11:28:54 +08:00
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:16:51 +08:00
// [
2021-11-06 01:47:00 +08:00
// [
// "pageId=1114219",
// "pageId",
// "1114219",
// ],
// [
// "searchId=8QC5D1D2E",
// "searchId",
// "8QC5D1D2E",
// ],
2021-11-06 01:16:51 +08:00
// ]
2021-11-04 21:39:28 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
func ExampleQuote() {
2021-11-05 23:56:08 +08:00
result := gregex.Quote(`[1-9]\d+`)
2021-11-16 21:40:04 +08:00
fmt.Println(result)
2021-11-01 12:17:53 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-16 21:40:04 +08:00
// \[1-9\]\\d\+
2021-11-01 12:17:53 +08:00
}
func ExampleReplace() {
2021-11-04 21:39:28 +08:00
var (
2021-11-06 00:49:15 +08:00
patternStr = `\d+`
2021-11-04 21:39:28 +08:00
str = "hello gf 2020!"
repStr = "2021"
result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str))
)
2021-11-03 11:22:17 +08:00
g.Dump(err)
g.Dump(result)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-04 21:39:28 +08:00
// <nil>
// "hello gf 2021!"
2021-11-01 12:17:53 +08:00
}
func ExampleReplaceFunc() {
2021-11-05 23:56:08 +08:00
// In contrast to [ExampleReplaceFunc]
// the result contains the `pattern' of all subpattern that use the matching function
result, err := gregex.ReplaceFuncMatch(`(\d+)~(\d+)`, []byte("hello gf 2018~2020!"), func(match [][]byte) []byte {
g.Dump(match)
match[2] = []byte("2021")
return bytes.Join(match[1:], []byte("~"))
2021-11-02 20:08:15 +08:00
})
2021-11-03 11:22:17 +08:00
g.Dump(result)
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 01:16:51 +08:00
// Output:
2021-11-05 23:56:08 +08:00
// [
2021-11-06 01:47:00 +08:00
// "2018~2020",
// "2018",
// "2020",
2021-11-05 23:56:08 +08:00
// ]
2021-11-04 21:39:28 +08:00
// "hello gf 2018~2021!"
2021-11-06 00:06:56 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
func ExampleReplaceFuncMatch() {
2021-11-04 21:39:28 +08:00
var (
2021-11-05 23:56:08 +08:00
patternStr = `(\d+)~(\d+)`
2021-11-04 21:39:28 +08:00
str = "hello gf 2018~2020!"
)
2021-11-03 11:22:17 +08:00
// In contrast to [ExampleReplaceFunc]
// the result contains the `pattern' of all subpatterns that use the matching function
2021-11-02 20:08:15 +08:00
result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte {
2021-11-03 11:22:17 +08:00
g.Dump(match)
2021-11-05 23:56:08 +08:00
match[2] = []byte("2021")
return bytes.Join(match[1:], []byte("-"))
2021-11-02 20:08:15 +08:00
})
2021-11-03 11:22:17 +08:00
g.Dump(result)
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:16:51 +08:00
// [
2021-11-06 01:47:00 +08:00
// "2018~2020",
// "2018",
// "2020",
2021-11-06 01:16:51 +08:00
// ]
2021-11-05 23:56:08 +08:00
// "hello gf 2018-2021!"
2021-11-04 21:39:28 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
func ExampleReplaceString() {
2021-11-06 00:49:15 +08:00
patternStr := `\d+`
2021-11-02 20:08:15 +08:00
str := "hello gf 2020!"
replaceStr := "2021"
result, err := gregex.ReplaceString(patternStr, replaceStr, str)
2021-11-03 11:22:17 +08:00
g.Dump(result)
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-04 21:39:28 +08:00
// "hello gf 2021!"
// <nil>
2021-11-01 12:17:53 +08:00
}
func ExampleReplaceStringFunc() {
2021-11-05 23:56:08 +08:00
replaceStrMap := map[string]string{
"2020": "2021",
}
2021-11-03 11:22:17 +08:00
// When the regular statement can match multiple results
// func can be used to further control the value that needs to be modified
2021-11-06 00:49:15 +08:00
result, err := gregex.ReplaceStringFunc(`\d+`, `hello gf 2018~2020!`, func(b string) string {
2021-11-03 11:22:17 +08:00
g.Dump(b)
2021-11-04 21:39:28 +08:00
if replaceStr, ok := replaceStrMap[b]; ok {
2021-11-02 20:08:15 +08:00
return replaceStr
}
2021-11-03 11:22:17 +08:00
return b
2021-11-02 20:08:15 +08:00
})
2021-11-03 11:22:17 +08:00
g.Dump(result)
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-05 23:56:08 +08:00
result, err = gregex.ReplaceStringFunc(`[a-z]*`, "gf@goframe.org", strings.ToUpper)
g.Dump(result)
g.Dump(err)
2021-11-06 00:54:10 +08:00
// Output:
2021-11-04 21:39:28 +08:00
// "2018"
// "2020"
// "hello gf 2018~2021!"
// <nil>
2021-11-05 23:56:08 +08:00
// "GF@GOFRAME.ORG"
// <nil>
2021-11-01 12:17:53 +08:00
}
func ExampleReplaceStringFuncMatch() {
2021-11-04 21:39:28 +08:00
var (
2021-11-05 23:56:08 +08:00
patternStr = `([A-Z])\w+`
str = "hello Golang 2018~2021!"
2021-11-04 21:39:28 +08:00
)
2021-11-05 23:56:08 +08:00
// 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]
2021-11-02 20:08:15 +08:00
})
2021-11-03 11:22:17 +08:00
g.Dump(result)
g.Dump(err)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:16:51 +08:00
// [
2021-11-06 01:47:00 +08:00
// "Golang",
// "G",
2021-11-06 01:16:51 +08:00
// ]
2021-11-05 23:56:08 +08:00
// "hello Gf 2018~2021!"
2021-11-04 21:39:28 +08:00
// <nil>
2021-11-01 12:17:53 +08:00
}
func ExampleSplit() {
2021-11-06 00:49:15 +08:00
patternStr := `\d+`
2021-11-02 20:08:15 +08:00
str := "hello2020gf"
result := gregex.Split(patternStr, str)
2021-11-03 11:22:17 +08:00
g.Dump(result)
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-06 01:16:51 +08:00
// [
2021-11-06 01:47:00 +08:00
// "hello",
// "gf",
2021-11-06 01:16:51 +08:00
// ]
2021-11-01 12:17:53 +08:00
}
func ExampleValidate() {
2021-11-03 11:22:17 +08:00
// Valid match statement
2021-11-16 21:40:04 +08:00
fmt.Println(gregex.Validate(`\d+`))
2021-11-03 11:22:17 +08:00
// Mismatched statement
2021-11-16 21:40:04 +08:00
fmt.Println(gregex.Validate(`[a-9]\d+`))
2021-11-02 20:08:15 +08:00
2021-11-06 00:54:10 +08:00
// Output:
2021-11-04 21:39:28 +08:00
// <nil>
// regexp.Compile failed for pattern "[a-9]\d+": error parsing regexp: invalid character class range: `a-9`
2021-11-01 12:17:53 +08:00
}