mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
Merge branch 'master' of https://github.com/gogf/gf
This commit is contained in:
@ -7,17 +7,401 @@
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/container/gset"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// New create and returns a new set, which contains un-repeated items.
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety,
|
||||
// which is false in default.
|
||||
func ExampleNewIntSet() {
|
||||
intSet := gset.NewIntSet()
|
||||
intSet.Add([]int{1, 2, 3}...)
|
||||
fmt.Println(intSet.Slice())
|
||||
|
||||
// May Output:
|
||||
// [2 1 3]
|
||||
}
|
||||
|
||||
// NewIntSetFrom returns a new set from `items`.
|
||||
func ExampleNewFrom() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
fmt.Println(intSet.Slice())
|
||||
|
||||
// May Output:
|
||||
// [2 1 3]
|
||||
}
|
||||
|
||||
// Add adds one or multiple items to the set.
|
||||
func ExampleIntSet_Add() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
intSet.Add(1)
|
||||
fmt.Println(intSet.Slice())
|
||||
fmt.Println(intSet.AddIfNotExist(1))
|
||||
|
||||
// Mya Output:
|
||||
// [1 2 3]
|
||||
// false
|
||||
}
|
||||
|
||||
// AddIfNotExist checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set,
|
||||
// or else it does nothing and returns false.
|
||||
func ExampleIntSet_AddIfNotExist() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
intSet.Add(1)
|
||||
fmt.Println(intSet.Slice())
|
||||
fmt.Println(intSet.AddIfNotExist(1))
|
||||
|
||||
// Mya Output:
|
||||
// [1 2 3]
|
||||
// false
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
|
||||
// or else it does nothing and returns false.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func ExampleIntSet_AddIfNotExistFunc() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
intSet.Add(1)
|
||||
fmt.Println(intSet.Slice())
|
||||
fmt.Println(intSet.AddIfNotExistFunc(5, func() bool {
|
||||
return true
|
||||
}))
|
||||
|
||||
// May Output:
|
||||
// [1 2 3]
|
||||
// true
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
|
||||
// or else it does nothing and returns false.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func ExampleIntSet_AddIfNotExistFuncLock() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
intSet.Add(1)
|
||||
fmt.Println(intSet.Slice())
|
||||
fmt.Println(intSet.AddIfNotExistFuncLock(4, func() bool {
|
||||
return true
|
||||
}))
|
||||
|
||||
// May Output:
|
||||
// [1 2 3]
|
||||
// true
|
||||
}
|
||||
|
||||
// Clear deletes all items of the set.
|
||||
func ExampleIntSet_Clear() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
fmt.Println(intSet.Size())
|
||||
intSet.Clear()
|
||||
fmt.Println(intSet.Size())
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
// 0
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from `set` to `full`.
|
||||
// Which means, all the items in `newSet` are in `full` and not in `set`.
|
||||
// It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
|
||||
func ExampleIntSet_Complement() {
|
||||
intSet := gset.NewIntSetFrom([]int{1, 2, 3, 4, 5})
|
||||
s := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
fmt.Println(s.Complement(intSet).Slice())
|
||||
|
||||
// May Output:
|
||||
// [4 5]
|
||||
}
|
||||
|
||||
// Contains checks whether the set contains `item`.
|
||||
func ExampleIntSet_Contains() {
|
||||
var set gset.IntSet
|
||||
set.Add(1)
|
||||
fmt.Println(set.Contains(1))
|
||||
fmt.Println(set.Contains(2))
|
||||
var set1 gset.IntSet
|
||||
set1.Add(1, 4, 5, 6, 7)
|
||||
fmt.Println(set1.Contains(1))
|
||||
|
||||
var set2 gset.IntSet
|
||||
set2.Add(1, 4, 5, 6, 7)
|
||||
fmt.Println(set2.Contains(8))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from `set` to `other`.
|
||||
// Which means, all the items in `newSet` are in `set` but not in `other`.
|
||||
func ExampleIntSet_Diff() {
|
||||
s1 := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
|
||||
fmt.Println(s2.Diff(s1).Slice())
|
||||
|
||||
// Output:
|
||||
// [4]
|
||||
}
|
||||
|
||||
// Equal checks whether the two sets equal.
|
||||
func ExampleIntSet_Equal() {
|
||||
s1 := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
|
||||
fmt.Println(s2.Equal(s1))
|
||||
|
||||
s3 := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
s4 := gset.NewIntSetFrom([]int{1, 2, 3})
|
||||
fmt.Println(s3.Equal(s4))
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from `set` to `other`.
|
||||
// Which means, all the items in `newSet` are in `set` and also in `other`.
|
||||
func ExampleIntSet_Intersect() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3}...)
|
||||
var s2 gset.IntSet
|
||||
s2.Add([]int{1, 2, 3, 4}...)
|
||||
fmt.Println(s2.Intersect(s1).Slice())
|
||||
|
||||
// May Output:
|
||||
// [1 2 3]
|
||||
}
|
||||
|
||||
// IsSubsetOf checks whether the current set is a sub-set of `other`
|
||||
func ExampleIntSet_IsSubsetOf() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
var s2 gset.IntSet
|
||||
s2.Add([]int{1, 2, 4}...)
|
||||
fmt.Println(s2.IsSubsetOf(s1))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func ExampleIntSet_Iterator() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
s1.Iterator(func(v int) bool {
|
||||
fmt.Println("Iterator", v)
|
||||
return true
|
||||
})
|
||||
// May Output:
|
||||
// Iterator 2
|
||||
// Iterator 3
|
||||
// Iterator 1
|
||||
// Iterator 4
|
||||
}
|
||||
|
||||
// Join joins items with a string `glue`.
|
||||
func ExampleIntSet_Join() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
fmt.Println(s1.Join(","))
|
||||
|
||||
// May Output:
|
||||
// 3,4,1,2
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function `f`.
|
||||
func ExampleIntSet_LockFunc() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2}...)
|
||||
s1.LockFunc(func(m map[int]struct{}) {
|
||||
m[3] = struct{}{}
|
||||
})
|
||||
fmt.Println(s1.Slice())
|
||||
|
||||
// May Output
|
||||
// [2 3 1]
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func ExampleIntSet_MarshalJSON() {
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
|
||||
// May Output:
|
||||
// {"Id":1,"Name":"john","Scores":[100,99,98]}
|
||||
}
|
||||
|
||||
// Merge adds items from `others` sets into `set`.
|
||||
func ExampleIntSet_Merge() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
|
||||
s2 := gset.NewIntSet()
|
||||
fmt.Println(s1.Merge(s2).Slice())
|
||||
|
||||
// May Output:
|
||||
// [1 2 3 4]
|
||||
}
|
||||
|
||||
// Pops randomly pops an item from set.
|
||||
func ExampleIntSet_Pop() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
|
||||
fmt.Println(s1.Pop())
|
||||
|
||||
// May Output:
|
||||
// 1
|
||||
}
|
||||
|
||||
// Pops randomly pops `size` items from set.
|
||||
// It returns all items if size == -1.
|
||||
func ExampleIntSet_Pops() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
for _, v := range s1.Pops(2) {
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
// May Output:
|
||||
// 1
|
||||
// 2
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with callback function `f`.
|
||||
func ExampleIntSet_RLockFunc() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
s1.RLockFunc(func(m map[int]struct{}) {
|
||||
fmt.Println(m)
|
||||
})
|
||||
|
||||
// Output:
|
||||
// map[1:{} 2:{} 3:{} 4:{}]
|
||||
}
|
||||
|
||||
// Remove deletes `item` from set.
|
||||
func ExampleIntSet_Remove() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
s1.Remove(1)
|
||||
fmt.Println(s1.Slice())
|
||||
|
||||
// May Output:
|
||||
// [3 4 2]
|
||||
}
|
||||
|
||||
// Size returns the size of the set.
|
||||
func ExampleIntSet_Size() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
fmt.Println(s1.Size())
|
||||
|
||||
// Output:
|
||||
// 4
|
||||
}
|
||||
|
||||
// Slice returns the a of items of the set as slice.
|
||||
func ExampleIntSet_Slice() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
fmt.Println(s1.Slice())
|
||||
|
||||
// May Output:
|
||||
// [1, 2, 3, 4]
|
||||
}
|
||||
|
||||
// String returns items as a string, which implements like json.Marshal does.
|
||||
func ExampleIntSet_String() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
fmt.Println(s1.String())
|
||||
|
||||
// May Output:
|
||||
// [1,2,3,4]
|
||||
}
|
||||
|
||||
// Sum sums items. Note: The items should be converted to int type,
|
||||
// or you'd get a result that you unexpected.
|
||||
func ExampleIntSet_Sum() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
fmt.Println(s1.Sum())
|
||||
|
||||
// Output:
|
||||
// 10
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of `set` and `other`.
|
||||
// Which means, all the items in `newSet` are in `set` or in `other`.
|
||||
func ExampleIntSet_Union() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add([]int{1, 2, 3, 4}...)
|
||||
s2 := gset.NewIntSet()
|
||||
s2.Add([]int{1, 2, 4}...)
|
||||
fmt.Println(s1.Union(s2).Slice())
|
||||
|
||||
// May Output:
|
||||
// [3 4 1 2]
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func ExampleIntSet_UnmarshalJSON() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
|
||||
// May Output:
|
||||
// {1 john [100,99,98]}
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for set.
|
||||
func ExampleIntSet_UnmarshalValue() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":100,99,98}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
|
||||
// May Output:
|
||||
// {1 john [100,99,98]}
|
||||
}
|
||||
|
||||
// Walk applies a user supplied function `f` to every item of set.
|
||||
func ExampleIntSet_Walk() {
|
||||
var (
|
||||
set gset.IntSet
|
||||
names = g.SliceInt{1, 0}
|
||||
delta = 10
|
||||
)
|
||||
set.Add(names...)
|
||||
// Add prefix for given table names.
|
||||
set.Walk(func(item int) int {
|
||||
return delta + item
|
||||
})
|
||||
fmt.Println(set.Slice())
|
||||
|
||||
// May Output:
|
||||
// [12 60]
|
||||
}
|
||||
|
||||
280
text/gregex/gregex_z_example_test.go
Normal file
280
text/gregex/gregex_z_example_test.go
Normal file
@ -0,0 +1,280 @@
|
||||
// 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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func ExampleIsMatchString() {
|
||||
patternStr := `\d+`
|
||||
g.Dump(gregex.IsMatchString(patternStr, "hello 2022! hello gf!"))
|
||||
g.Dump(gregex.IsMatchString(patternStr, "hello gf!"))
|
||||
g.Dump(gregex.IsMatchString(patternStr, ""))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
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",
|
||||
// ],
|
||||
// ]
|
||||
// <nil>
|
||||
}
|
||||
|
||||
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",
|
||||
// ],
|
||||
// ]
|
||||
// <nil>
|
||||
}
|
||||
|
||||
func ExampleQuote() {
|
||||
result := gregex.Quote(`[1-9]\d+`)
|
||||
g.Dump(result)
|
||||
|
||||
// Output:
|
||||
// "\[1-9\]\\d\+"
|
||||
}
|
||||
|
||||
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!"
|
||||
}
|
||||
|
||||
func ExampleReplaceFunc() {
|
||||
// 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",
|
||||
// "2018",
|
||||
// "2020",
|
||||
// ]
|
||||
// "hello gf 2018~2021!"
|
||||
// <nil>
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
func ExampleReplaceString() {
|
||||
patternStr := `\d+`
|
||||
str := "hello gf 2020!"
|
||||
replaceStr := "2021"
|
||||
result, err := gregex.ReplaceString(patternStr, replaceStr, str)
|
||||
|
||||
g.Dump(result)
|
||||
g.Dump(err)
|
||||
|
||||
// Output:
|
||||
// "hello gf 2021!"
|
||||
// <nil>
|
||||
}
|
||||
|
||||
func ExampleReplaceStringFunc() {
|
||||
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(`\d+`, `hello gf 2018~2020!`, func(b string) string {
|
||||
g.Dump(b)
|
||||
if replaceStr, ok := replaceStrMap[b]; ok {
|
||||
return replaceStr
|
||||
}
|
||||
return b
|
||||
})
|
||||
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!"
|
||||
// <nil>
|
||||
// "GF@GOFRAME.ORG"
|
||||
// <nil>
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
func ExampleSplit() {
|
||||
patternStr := `\d+`
|
||||
str := "hello2020gf"
|
||||
result := gregex.Split(patternStr, str)
|
||||
g.Dump(result)
|
||||
|
||||
// Output:
|
||||
// [
|
||||
// "hello",
|
||||
// "gf",
|
||||
// ]
|
||||
}
|
||||
|
||||
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",
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user