From 183d800f4cf5a5083003a51092972bbfea826dc1 Mon Sep 17 00:00:00 2001 From: daguang Date: Mon, 1 Nov 2021 12:17:53 +0800 Subject: [PATCH 01/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 text/gregex/gregex_z_example_test.go diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go new file mode 100644 index 000000000..8c5ea8e74 --- /dev/null +++ b/text/gregex/gregex_z_example_test.go @@ -0,0 +1,67 @@ +// 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 + +func ExampleIsMatch() { + +} + +func ExampleIsMatchString() { + +} + +func ExampleMatch() { + +} + +func ExampleMatchAll() { + +} + +func ExampleMatchAllString() { + +} + +func ExampleMatchString() { + +} + +func ExampleQuote() { + +} + +func ExampleReplace() { + +} + +func ExampleReplaceFunc() { + +} + +func ExampleReplaceFuncMatch() { + +} + +func ExampleReplaceString() { + +} + +func ExampleReplaceStringFunc() { + +} + +func ExampleReplaceStringFuncMatch() { + +} + +func ExampleSplit() { + +} + +func ExampleValidate() { + +} From 6c3aa6ede51deb577347768ced2d4cc76044284a Mon Sep 17 00:00:00 2001 From: daguang Date: Tue, 2 Nov 2021 20:08:15 +0800 Subject: [PATCH 02/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 174 ++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 2 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 8c5ea8e74..a4590ea63 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -3,65 +3,235 @@ // 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 -func ExampleIsMatch() { +import ( + "fmt" + "github.com/gogf/gf/v2/text/gregex" +) +func ExampleIsMatch() { + var str = "hello 94 easy gf!" + patternStr := `[1-9]\d*` + fmt.Println(gregex.IsMatch(patternStr, []byte(str))) + + // output + // true } func ExampleIsMatchString() { + var str = "hello 94 easy gf!" + patternStr := `[1-9]\d*` + fmt.Println(gregex.IsMatchString(patternStr, str)) + // output + // true } func ExampleMatch() { + var str = "hello 94 easy gf!" + patternStr := `[1-9]\d*` + result, err := gregex.Match(patternStr, []byte(str)) + if err != nil { + fmt.Println(err.Error()) + } + for _, v := range result { + fmt.Println(string(v)) + } + // output + // 94 } func ExampleMatchAll() { + var str = "hello 94 easy gf!" + patternStr := `[1-9]\d*` + results, err := gregex.MatchAll(patternStr, []byte(str)) + if err != nil { + fmt.Println(err.Error()) + } + for _, result := range results { + for _, v := range result { + fmt.Println(string(v)) + } + } + // output + // 94 } func ExampleMatchAllString() { + var str = "hello 94 easy gf!" + patternStr := `[1-9]\d*` + results, err := gregex.MatchAllString(patternStr, str) + if err != nil { + fmt.Println(err.Error()) + } + for _, result := range results { + for _, v := range result { + fmt.Println(v) + } + } + // output + // 94 } func ExampleMatchString() { + var str = "hello 94 easy gf!" + patternStr := `[1-9]\d*` + results, err := gregex.MatchString(patternStr, str) + if err != nil { + fmt.Println(err.Error()) + } + for _, result := range results { + fmt.Println(result) + } + // output + // 94 } func ExampleQuote() { + patternStr := `[1-9]\d*` + result := gregex.Quote(patternStr) + fmt.Println(result) + // output + // \[1-9\]\\d\* } func ExampleReplace() { + patternStr := `[1-9]\d*` + str := "hello gf 2020!" + repStr := "2021" + result, err := gregex.Replace(patternStr, []byte(repStr), []byte(str)) + if err != nil { + fmt.Println(err.Error()) + } + fmt.Println(string(result)) + // output + // hello gf 2021! } func ExampleReplaceFunc() { + patternStr := `[1-9]\d*` + str := "hello gf 2020!" + result, err := gregex.ReplaceFunc(patternStr, []byte(str), func(b []byte) []byte { + replaceStr := "2021" + if string(b) == "2020" { + return []byte(replaceStr) + } + return nil + }) + if err != nil { + fmt.Println(err.Error()) + } + fmt.Println(string(result)) + // output + // hello gf 2021! } func ExampleReplaceFuncMatch() { + patternStr := `[1-9]\d*` + str := "hello gf 2020!" + result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte { + replaceStr := "2021" + for _, v := range match { + if string(v) == "2020" { + return []byte(replaceStr) + } + } + return nil + }) + if err != nil { + fmt.Println(err.Error()) + } + fmt.Println(string(result)) + // output + // hello gf 2021! } func ExampleReplaceString() { + patternStr := `[1-9]\d*` + str := "hello gf 2020!" + replaceStr := "2021" + result, err := gregex.ReplaceString(patternStr, replaceStr, str) + if err != nil { + fmt.Println(result) + } + fmt.Println(result) + // output + // hello gf 2021! } func ExampleReplaceStringFunc() { + patternStr := `[1-9]\d*` + str := "hello gf 2020!" + result, err := gregex.ReplaceStringFunc(patternStr, str, func(s string) string { + replaceStr := "2021" + if s == "2020" { + return replaceStr + } + return "" + }) + if err != nil { + fmt.Println(result) + } + fmt.Println(result) + // output + // hello gf 2021! } func ExampleReplaceStringFuncMatch() { + patternStr := `[1-9]\d*` + str := "hello gf 2020!" + result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string { + replaceStr := "2021" + for _, v := range match { + if v == "2020" { + return replaceStr + } + } + return "" + }) + if err != nil { + fmt.Println(result) + } + fmt.Println(result) + // output + // hello gf 2021! } func ExampleSplit() { + patternStr := `[1-9]\d*` + str := "hello2020gf" + result := gregex.Split(patternStr, str) + for _, v := range result { + fmt.Println(v) + } + // output + // hello + // gf } func ExampleValidate() { + patternStr := `[1-9]\d*` + err := gregex.Validate(patternStr) + if err != nil { + fmt.Println(err) + } + if err == nil { + fmt.Println("ok") + } + // output + // ok } From d980cff6637b8bc0ca556409fc273d46c56906c6 Mon Sep 17 00:00:00 2001 From: daguang Date: Wed, 3 Nov 2021 11:22:17 +0800 Subject: [PATCH 03/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 182 ++++++++++++++++----------- 1 file changed, 107 insertions(+), 75 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index a4590ea63..4e2e7470d 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -7,6 +7,7 @@ package gregex_test import ( "fmt" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gregex" ) @@ -35,11 +36,11 @@ func ExampleMatch() { if err != nil { fmt.Println(err.Error()) } - for _, v := range result { - fmt.Println(string(v)) - } + g.Dump(result) + g.Dump(result[0]) // output + // ["OTQ="] // 94 } @@ -50,46 +51,41 @@ func ExampleMatchAll() { if err != nil { fmt.Println(err.Error()) } - for _, result := range results { - for _, v := range result { - fmt.Println(string(v)) - } - } + g.Dump(results) // output - // 94 + // [["OTQ="]] + // } func ExampleMatchAllString() { - var str = "hello 94 easy gf!" + var str = "hello 94 98 easy gf!" patternStr := `[1-9]\d*` results, err := gregex.MatchAllString(patternStr, str) if err != nil { fmt.Println(err.Error()) } - for _, result := range results { - for _, v := range result { - fmt.Println(v) - } - } + + g.Dump(results) // output - // 94 + // [["94"],["98"]] } func ExampleMatchString() { - var str = "hello 94 easy gf!" + var str = "hello 94 98 easy gf!" patternStr := `[1-9]\d*` + + // if you need a greed match, should use <..all> methods results, err := gregex.MatchString(patternStr, str) if err != nil { fmt.Println(err.Error()) } - for _, result := range results { - fmt.Println(result) - } + + g.Dump(results) // output - // 94 + // ["94"] } func ExampleQuote() { @@ -106,53 +102,75 @@ func ExampleReplace() { str := "hello gf 2020!" repStr := "2021" result, err := gregex.Replace(patternStr, []byte(repStr), []byte(str)) - if err != nil { - fmt.Println(err.Error()) - } - fmt.Println(string(result)) + + g.Dump(err) + g.Dump(result) // output + // null // hello gf 2021! } func ExampleReplaceFunc() { patternStr := `[1-9]\d*` - str := "hello gf 2020!" + str := "hello gf 2018~2020!" + + // When the regular statement can match multiple results + // func can be used to further control the value that needs to be modified result, err := gregex.ReplaceFunc(patternStr, []byte(str), func(b []byte) []byte { + + g.Dump(b) + replaceStr := "2021" if string(b) == "2020" { return []byte(replaceStr) } - return nil + return b }) - if err != nil { - fmt.Println(err.Error()) - } - fmt.Println(string(result)) + + g.Dump(result) + g.Dump(err) // output - // hello gf 2021! + // 2018 + // 2020 + // hello gf 2018~2021! + // null } func ExampleReplaceFuncMatch() { patternStr := `[1-9]\d*` - str := "hello gf 2020!" + 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) + replaceStr := "2021" for _, v := range match { if string(v) == "2020" { return []byte(replaceStr) } } - return nil + return match[0] }) - if err != nil { - fmt.Println(err.Error()) - } - fmt.Println(string(result)) + + g.Dump(result) + g.Dump(err) // output - // hello gf 2021! + // [ + // "MjAxOA==" + // ] + // + // [ + // "MjAyMA==" + // ] + // + // hello gf 2018~2021! + // null } func ExampleReplaceString() { @@ -160,78 +178,92 @@ func ExampleReplaceString() { str := "hello gf 2020!" replaceStr := "2021" result, err := gregex.ReplaceString(patternStr, replaceStr, str) - if err != nil { - fmt.Println(result) - } - fmt.Println(result) + + g.Dump(result) + g.Dump(err) // output // hello gf 2021! + // null } func ExampleReplaceStringFunc() { patternStr := `[1-9]\d*` - str := "hello gf 2020!" - result, err := gregex.ReplaceStringFunc(patternStr, str, func(s string) string { + str := "hello gf 2018~2020!" + + // When the regular statement can match multiple results + // func can be used to further control the value that needs to be modified + result, err := gregex.ReplaceStringFunc(patternStr, str, func(b string) string { + + g.Dump(b) + replaceStr := "2021" - if s == "2020" { + if b == "2020" { return replaceStr } - return "" + return b }) - if err != nil { - fmt.Println(result) - } - fmt.Println(result) + + g.Dump(result) + g.Dump(err) // output - // hello gf 2021! + // 2018 + // 2020 + // hello gf 2018~2021! + // null } func ExampleReplaceStringFuncMatch() { patternStr := `[1-9]\d*` - str := "hello gf 2020!" - result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string { + str := "hello gf 2018~2020!" + + // When the regular statement can match multiple results + // func can be used to further control the value that needs to be modified + result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(b []string) string { + + g.Dump(b) + replaceStr := "2021" - for _, v := range match { + for _, v := range b { if v == "2020" { return replaceStr } } - return "" + return b[0] }) - if err != nil { - fmt.Println(result) - } - fmt.Println(result) + + g.Dump(result) + g.Dump(err) // output - // hello gf 2021! + // ["2018"] + // ["2020"] + // hello gf 2018~2021! + // null } func ExampleSplit() { patternStr := `[1-9]\d*` str := "hello2020gf" result := gregex.Split(patternStr, str) - for _, v := range result { - fmt.Println(v) - } + + g.Dump(result) // output - // hello - // gf + // ["hello","gf"] } func ExampleValidate() { - patternStr := `[1-9]\d*` - err := gregex.Validate(patternStr) - if err != nil { - fmt.Println(err) - } - if err == nil { - fmt.Println("ok") - } + // Valid match statement + g.Dump(gregex.Validate(`[1-9]\d*`)) + // Mismatched statement + g.Dump(gregex.Validate(`[a-9]\d*`)) // output - // ok + // null + // { + // "Code": "invalid character class range", + // "Expr": "a-9" + // } } From 6b9f72d97336a0bcc035e6251de6e242d90f35f9 Mon Sep 17 00:00:00 2001 From: daguang Date: Wed, 3 Nov 2021 11:28:54 +0800 Subject: [PATCH 04/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 89 +++++++++++++++++++--------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 4e2e7470d..3ab980f63 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -6,70 +6,101 @@ package gregex_test import ( - "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gregex" ) func ExampleIsMatch() { - var str = "hello 94 easy gf!" patternStr := `[1-9]\d*` - fmt.Println(gregex.IsMatch(patternStr, []byte(str))) + g.Dump(gregex.IsMatch(patternStr, []byte("hello 94 easy gf!"))) + g.Dump(gregex.IsMatch(patternStr, nil)) + g.Dump(gregex.IsMatch(patternStr, []byte("hello easy gf!"))) // output // true + // false + // false } func ExampleIsMatchString() { - var str = "hello 94 easy gf!" patternStr := `[1-9]\d*` - fmt.Println(gregex.IsMatchString(patternStr, str)) + g.Dump(gregex.IsMatchString(patternStr, "hello 94 easy gf!")) + g.Dump(gregex.IsMatchString(patternStr, "hello easy gf!")) + g.Dump(gregex.IsMatchString(patternStr, "")) // output // true + // false + // false } func ExampleMatch() { - var str = "hello 94 easy gf!" patternStr := `[1-9]\d*` - result, err := gregex.Match(patternStr, []byte(str)) - if err != nil { - fmt.Println(err.Error()) - } + result, err := gregex.Match(patternStr, []byte("hello 94 98 easy gf!")) g.Dump(result) - g.Dump(result[0]) + g.Dump(err) + + result, err = gregex.Match(patternStr, nil) + g.Dump(result) + g.Dump(err) + + result, err = gregex.Match(patternStr, []byte("hello easy gf!")) + g.Dump(result) + g.Dump(err) // output // ["OTQ="] - // 94 + // null + // [] + // null + // [] + // null } func ExampleMatchAll() { - var str = "hello 94 easy gf!" patternStr := `[1-9]\d*` - results, err := gregex.MatchAll(patternStr, []byte(str)) - if err != nil { - fmt.Println(err.Error()) - } + results, err := gregex.MatchAll(patternStr, []byte("hello 94 98 easy gf!")) g.Dump(results) + g.Dump(err) + + results, err = gregex.MatchAll(patternStr, []byte("hello easy gf!")) + g.Dump(results) + g.Dump(err) + + results, err = gregex.MatchAll(patternStr, nil) + g.Dump(results) + g.Dump(err) // output - // [["OTQ="]] - // + // [["OTQ="],["OTg="]] + // null + // [] + // null + // [] + // null } func ExampleMatchAllString() { - var str = "hello 94 98 easy gf!" patternStr := `[1-9]\d*` - results, err := gregex.MatchAllString(patternStr, str) - if err != nil { - fmt.Println(err.Error()) - } - + results, err := gregex.MatchAllString(patternStr, "hello 94 98 easy gf!") g.Dump(results) + g.Dump(err) + + results, err = gregex.MatchAllString(patternStr, "hello easy gf!") + g.Dump(results) + g.Dump(err) + + results, err = gregex.MatchAllString(patternStr, "") + g.Dump(results) + g.Dump(err) // output // [["94"],["98"]] + // null + // [] + // null + // [] + // null } func ExampleMatchString() { @@ -78,20 +109,20 @@ func ExampleMatchString() { // if you need a greed match, should use <..all> methods results, err := gregex.MatchString(patternStr, str) - if err != nil { - fmt.Println(err.Error()) - } g.Dump(results) + g.Dump(err) // output // ["94"] + // null } func ExampleQuote() { patternStr := `[1-9]\d*` result := gregex.Quote(patternStr) - fmt.Println(result) + + g.Dump(result) // output // \[1-9\]\\d\* From 205243e8b927d55953ca67537d068506c158330a Mon Sep 17 00:00:00 2001 From: daguang Date: Thu, 4 Nov 2021 21:02:54 +0800 Subject: [PATCH 05/12] update --- text/gregex/gregex_z_example_test.go | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 3ab980f63..b55e58be1 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -16,7 +16,7 @@ func ExampleIsMatch() { g.Dump(gregex.IsMatch(patternStr, nil)) g.Dump(gregex.IsMatch(patternStr, []byte("hello easy gf!"))) - // output + // Output // true // false // false @@ -28,7 +28,7 @@ func ExampleIsMatchString() { g.Dump(gregex.IsMatchString(patternStr, "hello easy gf!")) g.Dump(gregex.IsMatchString(patternStr, "")) - // output + // Output // true // false // false @@ -48,7 +48,7 @@ func ExampleMatch() { g.Dump(result) g.Dump(err) - // output + // Output // ["OTQ="] // null // [] @@ -71,7 +71,7 @@ func ExampleMatchAll() { g.Dump(results) g.Dump(err) - // output + // Output // [["OTQ="],["OTg="]] // null // [] @@ -94,7 +94,7 @@ func ExampleMatchAllString() { g.Dump(results) g.Dump(err) - // output + // Output // [["94"],["98"]] // null // [] @@ -113,7 +113,7 @@ func ExampleMatchString() { g.Dump(results) g.Dump(err) - // output + // Output // ["94"] // null } @@ -124,7 +124,7 @@ func ExampleQuote() { g.Dump(result) - // output + // Output // \[1-9\]\\d\* } @@ -137,7 +137,7 @@ func ExampleReplace() { g.Dump(err) g.Dump(result) - // output + // Output // null // hello gf 2021! } @@ -162,7 +162,7 @@ func ExampleReplaceFunc() { g.Dump(result) g.Dump(err) - // output + // Output // 2018 // 2020 // hello gf 2018~2021! @@ -191,7 +191,7 @@ func ExampleReplaceFuncMatch() { g.Dump(result) g.Dump(err) - // output + // Output // [ // "MjAxOA==" // ] @@ -213,7 +213,7 @@ func ExampleReplaceString() { g.Dump(result) g.Dump(err) - // output + // Output // hello gf 2021! // null } @@ -238,7 +238,7 @@ func ExampleReplaceStringFunc() { g.Dump(result) g.Dump(err) - // output + // Output // 2018 // 2020 // hello gf 2018~2021! @@ -267,7 +267,7 @@ func ExampleReplaceStringFuncMatch() { g.Dump(result) g.Dump(err) - // output + // Output // ["2018"] // ["2020"] // hello gf 2018~2021! @@ -281,7 +281,7 @@ func ExampleSplit() { g.Dump(result) - // output + // Output // ["hello","gf"] } @@ -291,7 +291,7 @@ func ExampleValidate() { // Mismatched statement g.Dump(gregex.Validate(`[a-9]\d*`)) - // output + // Output // null // { // "Code": "invalid character class range", From 2a2bb4f1e1cc0f4e25cd55905ae70261bc4c8c2f Mon Sep 17 00:00:00 2001 From: daguang Date: Thu, 4 Nov 2021 21:39:28 +0800 Subject: [PATCH 06/12] improve the example document --- text/gregex/gregex_z_example_test.go | 291 +++++++++++++-------------- 1 file changed, 144 insertions(+), 147 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index b55e58be1..d4196470d 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -11,10 +11,10 @@ import ( ) func ExampleIsMatch() { - patternStr := `[1-9]\d*` - g.Dump(gregex.IsMatch(patternStr, []byte("hello 94 easy gf!"))) + patternStr := `[1-9]\d+` + g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello gf!"))) g.Dump(gregex.IsMatch(patternStr, nil)) - g.Dump(gregex.IsMatch(patternStr, []byte("hello easy gf!"))) + g.Dump(gregex.IsMatch(patternStr, []byte("hello gf!"))) // Output // true @@ -23,9 +23,9 @@ func ExampleIsMatch() { } func ExampleIsMatchString() { - patternStr := `[1-9]\d*` - g.Dump(gregex.IsMatchString(patternStr, "hello 94 easy gf!")) - g.Dump(gregex.IsMatchString(patternStr, "hello easy gf!")) + patternStr := `[1-9]\d+` + g.Dump(gregex.IsMatchString(patternStr, "hello 2022! hello gf!")) + g.Dump(gregex.IsMatchString(patternStr, "hello gf!")) g.Dump(gregex.IsMatchString(patternStr, "")) // Output @@ -35,8 +35,8 @@ func ExampleIsMatchString() { } func ExampleMatch() { - patternStr := `[1-9]\d*` - result, err := gregex.Match(patternStr, []byte("hello 94 98 easy gf!")) + patternStr := `[1-9]\d+` + result, err := gregex.Match(patternStr, []byte("hello 2022! hello gf!")) g.Dump(result) g.Dump(err) @@ -49,21 +49,21 @@ func ExampleMatch() { g.Dump(err) // Output - // ["OTQ="] - // null + // ["2022"] + // // [] - // null + // // [] - // null + // } func ExampleMatchAll() { - patternStr := `[1-9]\d*` - results, err := gregex.MatchAll(patternStr, []byte("hello 94 98 easy gf!")) + patternStr := `[1-9]\d+` + results, err := gregex.MatchAll(patternStr, []byte("goodBye 2021! hello 2022! hello gf!")) g.Dump(results) g.Dump(err) - results, err = gregex.MatchAll(patternStr, []byte("hello easy gf!")) + results, err = gregex.MatchAll(patternStr, []byte("hello gf!")) g.Dump(results) g.Dump(err) @@ -72,21 +72,21 @@ func ExampleMatchAll() { g.Dump(err) // Output - // [["OTQ="],["OTg="]] - // null + // [["2021"],["2022"]] + // // [] - // null + // // [] - // null + // } func ExampleMatchAllString() { - patternStr := `[1-9]\d*` - results, err := gregex.MatchAllString(patternStr, "hello 94 98 easy gf!") + patternStr := `[1-9]\d+` + results, err := gregex.MatchAllString(patternStr, "goodBye 2021! hello 2022! hello gf!") g.Dump(results) g.Dump(err) - results, err = gregex.MatchAllString(patternStr, "hello easy gf!") + results, err = gregex.MatchAllString(patternStr, "hello gf!") g.Dump(results) g.Dump(err) @@ -95,18 +95,17 @@ func ExampleMatchAllString() { g.Dump(err) // Output - // [["94"],["98"]] - // null + // [["2021"],["2022"]] + // // [] - // null + // // [] - // null + // } func ExampleMatchString() { - var str = "hello 94 98 easy gf!" - patternStr := `[1-9]\d*` - + var str = "goodBye 2021! hello 2022! hello gf!" + patternStr := `[1-9]\d+` // if you need a greed match, should use <..all> methods results, err := gregex.MatchString(patternStr, str) @@ -114,122 +113,50 @@ func ExampleMatchString() { g.Dump(err) // Output - // ["94"] - // null + // ["2021"] + // } func ExampleQuote() { - patternStr := `[1-9]\d*` + patternStr := `[1-9]\d+` result := gregex.Quote(patternStr) g.Dump(result) // Output - // \[1-9\]\\d\* + // "\[1-9\]\\d\+" } func ExampleReplace() { - patternStr := `[1-9]\d*` - str := "hello gf 2020!" - repStr := "2021" - result, err := gregex.Replace(patternStr, []byte(repStr), []byte(str)) + var ( + patternStr = `[1-9]\d+` + str = "hello gf 2020!" + repStr = "2021" + result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str)) + ) g.Dump(err) g.Dump(result) // Output - // null - // hello gf 2021! + // + // "hello gf 2021!" } func ExampleReplaceFunc() { - patternStr := `[1-9]\d*` - str := "hello gf 2018~2020!" + var ( + patternStr = `[1-9]\d+` + str = "hello gf 2018~2020!" + replaceStrMap = map[string][]byte{ + "2020": []byte("2021"), + } + ) // When the regular statement can match multiple results // func can be used to further control the value that needs to be modified result, err := gregex.ReplaceFunc(patternStr, []byte(str), func(b []byte) []byte { - g.Dump(b) - - replaceStr := "2021" - if string(b) == "2020" { - return []byte(replaceStr) - } - return b - }) - - g.Dump(result) - g.Dump(err) - - // Output - // 2018 - // 2020 - // hello gf 2018~2021! - // null -} - -func ExampleReplaceFuncMatch() { - patternStr := `[1-9]\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) - - replaceStr := "2021" - for _, v := range match { - if string(v) == "2020" { - return []byte(replaceStr) - } - } - return match[0] - }) - - g.Dump(result) - g.Dump(err) - - // Output - // [ - // "MjAxOA==" - // ] - // - // [ - // "MjAyMA==" - // ] - // - // hello gf 2018~2021! - // null -} - -func ExampleReplaceString() { - patternStr := `[1-9]\d*` - str := "hello gf 2020!" - replaceStr := "2021" - result, err := gregex.ReplaceString(patternStr, replaceStr, str) - - g.Dump(result) - g.Dump(err) - - // Output - // hello gf 2021! - // null -} - -func ExampleReplaceStringFunc() { - patternStr := `[1-9]\d*` - str := "hello gf 2018~2020!" - - // When the regular statement can match multiple results - // func can be used to further control the value that needs to be modified - result, err := gregex.ReplaceStringFunc(patternStr, str, func(b string) string { - - g.Dump(b) - - replaceStr := "2021" - if b == "2020" { + if replaceStr, ok := replaceStrMap[string(b)]; ok { return replaceStr } return b @@ -239,46 +166,116 @@ func ExampleReplaceStringFunc() { g.Dump(err) // Output - // 2018 - // 2020 - // hello gf 2018~2021! - // null + // "2018" + // "2020" + // "hello gf 2018~2021!" + // } -func ExampleReplaceStringFuncMatch() { - patternStr := `[1-9]\d*` - str := "hello gf 2018~2020!" - - // When the regular statement can match multiple results - // func can be used to further control the value that needs to be modified - result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(b []string) string { - - g.Dump(b) - - replaceStr := "2021" - for _, v := range b { - if v == "2020" { +func ExampleReplaceFuncMatch() { + var ( + patternStr = `[1-9]\d+` + str = "hello gf 2018~2020!" + replaceMap = map[string][]byte{ + "2020": []byte("2021"), + } + ) + // 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) + for _, v := range match { + replaceStr, ok := replaceMap[string(v)] + if ok { return replaceStr } } - return b[0] + return match[0] }) - g.Dump(result) g.Dump(err) // Output // ["2018"] // ["2020"] - // hello gf 2018~2021! - // null + // "hello gf 2018~2021!" + // +} + +func ExampleReplaceString() { + patternStr := `[1-9]\d+` + str := "hello gf 2020!" + replaceStr := "2021" + result, err := gregex.ReplaceString(patternStr, replaceStr, str) + + g.Dump(result) + g.Dump(err) + + // Output + // "hello gf 2021!" + // +} + +func ExampleReplaceStringFunc() { + var ( + patternStr = `[1-9]\d+` + str = "hello gf 2018~2020!" + replaceStrMap = map[string]string{ + "2020": "2021", + } + ) + // When the regular statement can match multiple results + // func can be used to further control the value that needs to be modified + result, err := gregex.ReplaceStringFunc(patternStr, str, func(b string) string { + g.Dump(b) + if replaceStr, ok := replaceStrMap[b]; ok { + return replaceStr + } + return b + }) + g.Dump(result) + g.Dump(err) + + // Output + // "2018" + // "2020" + // "hello gf 2018~2021!" + // +} + +func ExampleReplaceStringFuncMatch() { + var ( + patternStr = `[1-9]\d+` + str = "hello gf 2018~2020!" + replaceStrMap = map[string]string{ + "2020": "2021", + } + ) + // When the regular statement can match multiple results + // func can be used to further control the value that needs to be modified + result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(b []string) string { + g.Dump(b) + for _, v := range b { + if replaceStr, ok := replaceStrMap[v]; ok { + return replaceStr + } + } + return b[0] + }) + g.Dump(result) + g.Dump(err) + + // Output + // ["2018"] + // ["2020"] + // "hello gf 2018~2021!" + // } func ExampleSplit() { - patternStr := `[1-9]\d*` + patternStr := `[1-9]\d+` str := "hello2020gf" result := gregex.Split(patternStr, str) - g.Dump(result) // Output @@ -287,12 +284,12 @@ func ExampleSplit() { func ExampleValidate() { // Valid match statement - g.Dump(gregex.Validate(`[1-9]\d*`)) + g.Dump(gregex.Validate(`[1-9]\d+`)) // Mismatched statement - g.Dump(gregex.Validate(`[a-9]\d*`)) + g.Dump(gregex.Validate(`[a-9]\d+`)) // Output - // null + // // { // "Code": "invalid character class range", // "Expr": "a-9" From 24f759e00ea8017eaf0b94690e8c66a2389c8602 Mon Sep 17 00:00:00 2001 From: daguang Date: Fri, 5 Nov 2021 23:56:08 +0800 Subject: [PATCH 07/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 208 ++++++++++----------------- 1 file changed, 75 insertions(+), 133 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index d4196470d..af8612322 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -6,8 +6,10 @@ package gregex_test import ( + "bytes" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gregex" + "strings" ) func ExampleIsMatch() { @@ -35,92 +37,57 @@ func ExampleIsMatchString() { } func ExampleMatch() { - patternStr := `[1-9]\d+` - result, err := gregex.Match(patternStr, []byte("hello 2022! hello gf!")) - g.Dump(result) - g.Dump(err) - - result, err = gregex.Match(patternStr, nil) - g.Dump(result) - g.Dump(err) - - result, err = gregex.Match(patternStr, []byte("hello easy gf!")) + 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 - // ["2022"] - // - // [] - // - // [] - // -} - -func ExampleMatchAll() { - patternStr := `[1-9]\d+` - results, err := gregex.MatchAll(patternStr, []byte("goodBye 2021! hello 2022! hello gf!")) - g.Dump(results) - g.Dump(err) - - results, err = gregex.MatchAll(patternStr, []byte("hello gf!")) - g.Dump(results) - g.Dump(err) - - results, err = gregex.MatchAll(patternStr, nil) - g.Dump(results) - g.Dump(err) - - // Output - // [["2021"],["2022"]] - // - // [] - // - // [] - // -} - -func ExampleMatchAllString() { - patternStr := `[1-9]\d+` - results, err := gregex.MatchAllString(patternStr, "goodBye 2021! hello 2022! hello gf!") - g.Dump(results) - g.Dump(err) - - results, err = gregex.MatchAllString(patternStr, "hello gf!") - g.Dump(results) - g.Dump(err) - - results, err = gregex.MatchAllString(patternStr, "") - g.Dump(results) - g.Dump(err) - - // Output - // [["2021"],["2022"]] - // - // [] - // - // [] + // ["pageId=1114219","pageId","1114219"] // } func ExampleMatchString() { - var str = "goodBye 2021! hello 2022! hello gf!" - patternStr := `[1-9]\d+` - // if you need a greed match, should use <..all> methods - results, err := gregex.MatchString(patternStr, str) - - g.Dump(results) + 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 - // ["2021"] + // ["pageId=1114219","pageId","1114219"] + // +} + +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) + g.Dump(err) + + // Output + // [["pageId=1114219","pageId","1114219",],["searchId=8QC5D1D2E","searchId","8QC5D1D2E"]] + // +} + +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) + g.Dump(err) + + // Output + // [["pageId=1114219","pageId","1114219",],["searchId=8QC5D1D2E","searchId","8QC5D1D2E"]] // } func ExampleQuote() { - patternStr := `[1-9]\d+` - result := gregex.Quote(patternStr) - + result := gregex.Quote(`[1-9]\d+`) g.Dump(result) // Output @@ -134,7 +101,6 @@ func ExampleReplace() { repStr = "2021" result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str)) ) - g.Dump(err) g.Dump(result) @@ -144,61 +110,43 @@ func ExampleReplace() { } func ExampleReplaceFunc() { - var ( - patternStr = `[1-9]\d+` - str = "hello gf 2018~2020!" - replaceStrMap = map[string][]byte{ - "2020": []byte("2021"), - } - ) - - // When the regular statement can match multiple results - // func can be used to further control the value that needs to be modified - result, err := gregex.ReplaceFunc(patternStr, []byte(str), func(b []byte) []byte { - g.Dump(b) - if replaceStr, ok := replaceStrMap[string(b)]; ok { - return replaceStr - } - return b + // 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("~")) }) - g.Dump(result) g.Dump(err) - // Output - // "2018" - // "2020" + // Output: + // [ + // "2018~2020", + // "2018", + // "2020", + // ] // "hello gf 2018~2021!" - // } func ExampleReplaceFuncMatch() { var ( - patternStr = `[1-9]\d+` + patternStr = `(\d+)~(\d+)` str = "hello gf 2018~2020!" - replaceMap = map[string][]byte{ - "2020": []byte("2021"), - } ) // 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) - for _, v := range match { - replaceStr, ok := replaceMap[string(v)] - if ok { - return replaceStr - } - } - return match[0] + match[2] = []byte("2021") + return bytes.Join(match[1:], []byte("-")) }) g.Dump(result) g.Dump(err) // Output - // ["2018"] - // ["2020"] - // "hello gf 2018~2021!" + // ["2018~2020","2018","2020"] + // "hello gf 2018-2021!" // } @@ -217,16 +165,12 @@ func ExampleReplaceString() { } func ExampleReplaceStringFunc() { - var ( - patternStr = `[1-9]\d+` - str = "hello gf 2018~2020!" - replaceStrMap = map[string]string{ - "2020": "2021", - } - ) + replaceStrMap := map[string]string{ + "2020": "2021", + } // When the regular statement can match multiple results // func can be used to further control the value that needs to be modified - result, err := gregex.ReplaceStringFunc(patternStr, str, func(b string) string { + result, err := gregex.ReplaceStringFunc(`[1-9]\d+`, `hello gf 2018~2020!`, func(b string) string { g.Dump(b) if replaceStr, ok := replaceStrMap[b]; ok { return replaceStr @@ -236,39 +180,37 @@ func ExampleReplaceStringFunc() { g.Dump(result) g.Dump(err) + result, err = gregex.ReplaceStringFunc(`[a-z]*`, "gf@goframe.org", strings.ToUpper) + g.Dump(result) + g.Dump(err) + // Output // "2018" // "2020" // "hello gf 2018~2021!" // + // "GF@GOFRAME.ORG" + // } func ExampleReplaceStringFuncMatch() { var ( - patternStr = `[1-9]\d+` - str = "hello gf 2018~2020!" - replaceStrMap = map[string]string{ - "2020": "2021", - } + patternStr = `([A-Z])\w+` + str = "hello Golang 2018~2021!" ) - // When the regular statement can match multiple results - // func can be used to further control the value that needs to be modified - result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(b []string) string { - g.Dump(b) - for _, v := range b { - if replaceStr, ok := replaceStrMap[v]; ok { - return replaceStr - } - } - return b[0] + // 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 - // ["2018"] - // ["2020"] - // "hello gf 2018~2021!" + // ["Golang","G"] + // "hello Gf 2018~2021!" // } From 99d2186c8cfa4156bd5d4ba955eb0fcc2309da9f Mon Sep 17 00:00:00 2001 From: daguang Date: Sat, 6 Nov 2021 00:06:56 +0800 Subject: [PATCH 08/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index af8612322..99828f988 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -127,6 +127,7 @@ func ExampleReplaceFunc() { // "2020", // ] // "hello gf 2018~2021!" + // } func ExampleReplaceFuncMatch() { From 64fa8d52828ce0e6ae9b3872e97907fe8dc5343b Mon Sep 17 00:00:00 2001 From: daguang Date: Sat, 6 Nov 2021 00:49:15 +0800 Subject: [PATCH 09/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 99828f988..7c4a7857e 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -13,7 +13,7 @@ import ( ) func ExampleIsMatch() { - patternStr := `[1-9]\d+` + 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!"))) @@ -25,7 +25,7 @@ func ExampleIsMatch() { } func ExampleIsMatchString() { - patternStr := `[1-9]\d+` + patternStr := `\d+` g.Dump(gregex.IsMatchString(patternStr, "hello 2022! hello gf!")) g.Dump(gregex.IsMatchString(patternStr, "hello gf!")) g.Dump(gregex.IsMatchString(patternStr, "")) @@ -96,7 +96,7 @@ func ExampleQuote() { func ExampleReplace() { var ( - patternStr = `[1-9]\d+` + patternStr = `\d+` str = "hello gf 2020!" repStr = "2021" result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str)) @@ -152,7 +152,7 @@ func ExampleReplaceFuncMatch() { } func ExampleReplaceString() { - patternStr := `[1-9]\d+` + patternStr := `\d+` str := "hello gf 2020!" replaceStr := "2021" result, err := gregex.ReplaceString(patternStr, replaceStr, str) @@ -171,7 +171,7 @@ func ExampleReplaceStringFunc() { } // When the regular statement can match multiple results // func can be used to further control the value that needs to be modified - result, err := gregex.ReplaceStringFunc(`[1-9]\d+`, `hello gf 2018~2020!`, func(b string) string { + result, err := gregex.ReplaceStringFunc(`\d+`, `hello gf 2018~2020!`, func(b string) string { g.Dump(b) if replaceStr, ok := replaceStrMap[b]; ok { return replaceStr @@ -216,7 +216,7 @@ func ExampleReplaceStringFuncMatch() { } func ExampleSplit() { - patternStr := `[1-9]\d+` + patternStr := `\d+` str := "hello2020gf" result := gregex.Split(patternStr, str) g.Dump(result) @@ -227,7 +227,7 @@ func ExampleSplit() { func ExampleValidate() { // Valid match statement - g.Dump(gregex.Validate(`[1-9]\d+`)) + g.Dump(gregex.Validate(`\d+`)) // Mismatched statement g.Dump(gregex.Validate(`[a-9]\d+`)) From 1a596fe84d7996f6bccdc32965d09a02761c55b2 Mon Sep 17 00:00:00 2001 From: daguang Date: Sat, 6 Nov 2021 00:54:10 +0800 Subject: [PATCH 10/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 7c4a7857e..8acf97325 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -18,7 +18,7 @@ func ExampleIsMatch() { g.Dump(gregex.IsMatch(patternStr, nil)) g.Dump(gregex.IsMatch(patternStr, []byte("hello gf!"))) - // Output + // Output: // true // false // false @@ -30,7 +30,7 @@ func ExampleIsMatchString() { g.Dump(gregex.IsMatchString(patternStr, "hello gf!")) g.Dump(gregex.IsMatchString(patternStr, "")) - // Output + // Output: // true // false // false @@ -44,7 +44,7 @@ func ExampleMatch() { g.Dump(result) g.Dump(err) - // Output + // Output: // ["pageId=1114219","pageId","1114219"] // } @@ -57,7 +57,7 @@ func ExampleMatchString() { g.Dump(result) g.Dump(err) - // Output + // Output: // ["pageId=1114219","pageId","1114219"] // } @@ -69,7 +69,7 @@ func ExampleMatchAll() { g.Dump(result) g.Dump(err) - // Output + // Output: // [["pageId=1114219","pageId","1114219",],["searchId=8QC5D1D2E","searchId","8QC5D1D2E"]] // } @@ -81,7 +81,7 @@ func ExampleMatchAllString() { g.Dump(result) g.Dump(err) - // Output + // Output: // [["pageId=1114219","pageId","1114219",],["searchId=8QC5D1D2E","searchId","8QC5D1D2E"]] // } @@ -90,7 +90,7 @@ func ExampleQuote() { result := gregex.Quote(`[1-9]\d+`) g.Dump(result) - // Output + // Output: // "\[1-9\]\\d\+" } @@ -104,7 +104,7 @@ func ExampleReplace() { g.Dump(err) g.Dump(result) - // Output + // Output: // // "hello gf 2021!" } @@ -120,7 +120,7 @@ func ExampleReplaceFunc() { g.Dump(result) g.Dump(err) - // Output: + // Output:: // [ // "2018~2020", // "2018", @@ -145,7 +145,7 @@ func ExampleReplaceFuncMatch() { g.Dump(result) g.Dump(err) - // Output + // Output: // ["2018~2020","2018","2020"] // "hello gf 2018-2021!" // @@ -160,7 +160,7 @@ func ExampleReplaceString() { g.Dump(result) g.Dump(err) - // Output + // Output: // "hello gf 2021!" // } @@ -185,7 +185,7 @@ func ExampleReplaceStringFunc() { g.Dump(result) g.Dump(err) - // Output + // Output: // "2018" // "2020" // "hello gf 2018~2021!" @@ -209,7 +209,7 @@ func ExampleReplaceStringFuncMatch() { g.Dump(result) g.Dump(err) - // Output + // Output: // ["Golang","G"] // "hello Gf 2018~2021!" // @@ -221,7 +221,7 @@ func ExampleSplit() { result := gregex.Split(patternStr, str) g.Dump(result) - // Output + // Output: // ["hello","gf"] } @@ -231,7 +231,7 @@ func ExampleValidate() { // Mismatched statement g.Dump(gregex.Validate(`[a-9]\d+`)) - // Output + // Output: // // { // "Code": "invalid character class range", From b967cf62247c881a07d26a00a555a799f453cef8 Mon Sep 17 00:00:00 2001 From: daguang Date: Sat, 6 Nov 2021 01:16:51 +0800 Subject: [PATCH 11/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 66 ++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 8acf97325..5c0c55f4a 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -45,7 +45,11 @@ func ExampleMatch() { g.Dump(err) // Output: - // ["pageId=1114219","pageId","1114219"] + // [ + // "pageId=1114219", + // "pageId", + // "1114219", + // ] // } @@ -58,7 +62,11 @@ func ExampleMatchString() { g.Dump(err) // Output: - // ["pageId=1114219","pageId","1114219"] + // [ + // "pageId=1114219", + // "pageId", + // "1114219", + // ] // } @@ -70,7 +78,18 @@ func ExampleMatchAll() { g.Dump(err) // Output: - // [["pageId=1114219","pageId","1114219",],["searchId=8QC5D1D2E","searchId","8QC5D1D2E"]] + // [ + // [ + // "pageId=1114219", + // "pageId", + // "1114219", + // ], + // [ + // "searchId=8QC5D1D2E", + // "searchId", + // "8QC5D1D2E", + // ], + // ] // } @@ -82,7 +101,18 @@ func ExampleMatchAllString() { g.Dump(err) // Output: - // [["pageId=1114219","pageId","1114219",],["searchId=8QC5D1D2E","searchId","8QC5D1D2E"]] + // [ + // [ + // "pageId=1114219", + // "pageId", + // "1114219", + // ], + // [ + // "searchId=8QC5D1D2E", + // "searchId", + // "8QC5D1D2E", + // ], + // ] // } @@ -120,11 +150,11 @@ func ExampleReplaceFunc() { g.Dump(result) g.Dump(err) - // Output:: + // Output: // [ - // "2018~2020", - // "2018", - // "2020", + // "2018~2020", + // "2018", + // "2020", // ] // "hello gf 2018~2021!" // @@ -146,7 +176,11 @@ func ExampleReplaceFuncMatch() { g.Dump(err) // Output: - // ["2018~2020","2018","2020"] + // [ + // "2018~2020", + // "2018", + // "2020", + // ] // "hello gf 2018-2021!" // } @@ -210,7 +244,10 @@ func ExampleReplaceStringFuncMatch() { g.Dump(err) // Output: - // ["Golang","G"] + // [ + // "Golang", + // "G", + // ] // "hello Gf 2018~2021!" // } @@ -222,7 +259,10 @@ func ExampleSplit() { g.Dump(result) // Output: - // ["hello","gf"] + // [ + // "hello", + // "gf", + // ] } func ExampleValidate() { @@ -234,7 +274,7 @@ func ExampleValidate() { // Output: // // { - // "Code": "invalid character class range", - // "Expr": "a-9" + // Code: "invalid character class range", + // Expr: "a-9", // } } From b036767fc083b21c544bcbc2a49450a7450575b6 Mon Sep 17 00:00:00 2001 From: daguang Date: Sat, 6 Nov 2021 01:47:00 +0800 Subject: [PATCH 12/12] :memo: add example --- text/gregex/gregex_z_example_test.go | 78 ++++++++++++++-------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/text/gregex/gregex_z_example_test.go b/text/gregex/gregex_z_example_test.go index 5c0c55f4a..ecf6128d7 100644 --- a/text/gregex/gregex_z_example_test.go +++ b/text/gregex/gregex_z_example_test.go @@ -46,9 +46,9 @@ func ExampleMatch() { // Output: // [ - // "pageId=1114219", - // "pageId", - // "1114219", + // "pageId=1114219", + // "pageId", + // "1114219", // ] // } @@ -63,9 +63,9 @@ func ExampleMatchString() { // Output: // [ - // "pageId=1114219", - // "pageId", - // "1114219", + // "pageId=1114219", + // "pageId", + // "1114219", // ] // } @@ -78,17 +78,17 @@ func ExampleMatchAll() { g.Dump(err) // Output: - // [ - // [ - // "pageId=1114219", - // "pageId", - // "1114219", - // ], - // [ - // "searchId=8QC5D1D2E", - // "searchId", - // "8QC5D1D2E", - // ], + // [ + // [ + // "pageId=1114219", + // "pageId", + // "1114219", + // ], + // [ + // "searchId=8QC5D1D2E", + // "searchId", + // "8QC5D1D2E", + // ], // ] // } @@ -102,16 +102,16 @@ func ExampleMatchAllString() { // Output: // [ - // [ - // "pageId=1114219", - // "pageId", - // "1114219", - // ], - // [ - // "searchId=8QC5D1D2E", - // "searchId", - // "8QC5D1D2E", - // ], + // [ + // "pageId=1114219", + // "pageId", + // "1114219", + // ], + // [ + // "searchId=8QC5D1D2E", + // "searchId", + // "8QC5D1D2E", + // ], // ] // } @@ -152,9 +152,9 @@ func ExampleReplaceFunc() { // Output: // [ - // "2018~2020", - // "2018", - // "2020", + // "2018~2020", + // "2018", + // "2020", // ] // "hello gf 2018~2021!" // @@ -177,9 +177,9 @@ func ExampleReplaceFuncMatch() { // Output: // [ - // "2018~2020", - // "2018", - // "2020", + // "2018~2020", + // "2018", + // "2020", // ] // "hello gf 2018-2021!" // @@ -245,8 +245,8 @@ func ExampleReplaceStringFuncMatch() { // Output: // [ - // "Golang", - // "G", + // "Golang", + // "G", // ] // "hello Gf 2018~2021!" // @@ -260,8 +260,8 @@ func ExampleSplit() { // Output: // [ - // "hello", - // "gf", + // "hello", + // "gf", // ] } @@ -274,7 +274,7 @@ func ExampleValidate() { // Output: // // { - // Code: "invalid character class range", - // Expr: "a-9", + // Code: "invalid character class range", + // Expr: "a-9", // } }