github.com/wangyougui/gf/v2@v2.6.5/text/gregex/gregex_z_example_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 package gregex_test 7 8 import ( 9 "bytes" 10 "fmt" 11 "strings" 12 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/gf/v2/text/gregex" 15 ) 16 17 func ExampleIsMatch() { 18 patternStr := `\d+` 19 g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello gf!"))) 20 g.Dump(gregex.IsMatch(patternStr, nil)) 21 g.Dump(gregex.IsMatch(patternStr, []byte("hello gf!"))) 22 23 // Output: 24 // true 25 // false 26 // false 27 } 28 29 func ExampleIsMatchString() { 30 patternStr := `\d+` 31 g.Dump(gregex.IsMatchString(patternStr, "hello 2022! hello gf!")) 32 g.Dump(gregex.IsMatchString(patternStr, "hello gf!")) 33 g.Dump(gregex.IsMatchString(patternStr, "")) 34 35 // Output: 36 // true 37 // false 38 // false 39 } 40 41 func ExampleMatch() { 42 patternStr := `(\w+)=(\w+)` 43 matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!" 44 // This method looks for the first match index 45 result, err := gregex.Match(patternStr, []byte(matchStr)) 46 g.Dump(result) 47 g.Dump(err) 48 49 // Output: 50 // [ 51 // "pageId=1114219", 52 // "pageId", 53 // "1114219", 54 // ] 55 // <nil> 56 } 57 58 func ExampleMatchString() { 59 patternStr := `(\w+)=(\w+)` 60 matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!" 61 // This method looks for the first match index 62 result, err := gregex.MatchString(patternStr, matchStr) 63 g.Dump(result) 64 g.Dump(err) 65 66 // Output: 67 // [ 68 // "pageId=1114219", 69 // "pageId", 70 // "1114219", 71 // ] 72 // <nil> 73 } 74 75 func ExampleMatchAll() { 76 patternStr := `(\w+)=(\w+)` 77 matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!" 78 result, err := gregex.MatchAll(patternStr, []byte(matchStr)) 79 g.Dump(result) 80 g.Dump(err) 81 82 // Output: 83 // [ 84 // [ 85 // "pageId=1114219", 86 // "pageId", 87 // "1114219", 88 // ], 89 // [ 90 // "searchId=8QC5D1D2E", 91 // "searchId", 92 // "8QC5D1D2E", 93 // ], 94 // ] 95 // <nil> 96 } 97 98 func ExampleMatchAllString() { 99 patternStr := `(\w+)=(\w+)` 100 matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!" 101 result, err := gregex.MatchAllString(patternStr, matchStr) 102 g.Dump(result) 103 g.Dump(err) 104 105 // Output: 106 // [ 107 // [ 108 // "pageId=1114219", 109 // "pageId", 110 // "1114219", 111 // ], 112 // [ 113 // "searchId=8QC5D1D2E", 114 // "searchId", 115 // "8QC5D1D2E", 116 // ], 117 // ] 118 // <nil> 119 } 120 121 func ExampleQuote() { 122 result := gregex.Quote(`[1-9]\d+`) 123 fmt.Println(result) 124 125 // Output: 126 // \[1-9\]\\d\+ 127 } 128 129 func ExampleReplace() { 130 var ( 131 patternStr = `\d+` 132 str = "hello gf 2020!" 133 repStr = "2021" 134 result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str)) 135 ) 136 g.Dump(err) 137 g.Dump(result) 138 139 // Output: 140 // <nil> 141 // "hello gf 2021!" 142 } 143 144 func ExampleReplaceFunc() { 145 // In contrast to [ExampleReplaceFunc] 146 // the result contains the `pattern' of all subpattern that use the matching function 147 result, err := gregex.ReplaceFuncMatch(`(\d+)~(\d+)`, []byte("hello gf 2018~2020!"), func(match [][]byte) []byte { 148 g.Dump(match) 149 match[2] = []byte("2021") 150 return bytes.Join(match[1:], []byte("~")) 151 }) 152 g.Dump(result) 153 g.Dump(err) 154 155 // Output: 156 // [ 157 // "2018~2020", 158 // "2018", 159 // "2020", 160 // ] 161 // "hello gf 2018~2021!" 162 // <nil> 163 } 164 165 func ExampleReplaceFuncMatch() { 166 var ( 167 patternStr = `(\d+)~(\d+)` 168 str = "hello gf 2018~2020!" 169 ) 170 // In contrast to [ExampleReplaceFunc] 171 // the result contains the `pattern' of all subpatterns that use the matching function 172 result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte { 173 g.Dump(match) 174 match[2] = []byte("2021") 175 return bytes.Join(match[1:], []byte("-")) 176 }) 177 g.Dump(result) 178 g.Dump(err) 179 180 // Output: 181 // [ 182 // "2018~2020", 183 // "2018", 184 // "2020", 185 // ] 186 // "hello gf 2018-2021!" 187 // <nil> 188 } 189 190 func ExampleReplaceString() { 191 patternStr := `\d+` 192 str := "hello gf 2020!" 193 replaceStr := "2021" 194 result, err := gregex.ReplaceString(patternStr, replaceStr, str) 195 196 g.Dump(result) 197 g.Dump(err) 198 199 // Output: 200 // "hello gf 2021!" 201 // <nil> 202 } 203 204 func ExampleReplaceStringFunc() { 205 replaceStrMap := map[string]string{ 206 "2020": "2021", 207 } 208 // When the regular statement can match multiple results 209 // func can be used to further control the value that needs to be modified 210 result, err := gregex.ReplaceStringFunc(`\d+`, `hello gf 2018~2020!`, func(b string) string { 211 g.Dump(b) 212 if replaceStr, ok := replaceStrMap[b]; ok { 213 return replaceStr 214 } 215 return b 216 }) 217 g.Dump(result) 218 g.Dump(err) 219 220 result, err = gregex.ReplaceStringFunc(`[a-z]*`, "gf@goframe.org", strings.ToUpper) 221 g.Dump(result) 222 g.Dump(err) 223 224 // Output: 225 // "2018" 226 // "2020" 227 // "hello gf 2018~2021!" 228 // <nil> 229 // "GF@GOFRAME.ORG" 230 // <nil> 231 } 232 233 func ExampleReplaceStringFuncMatch() { 234 var ( 235 patternStr = `([A-Z])\w+` 236 str = "hello Golang 2018~2021!" 237 ) 238 // In contrast to [ExampleReplaceFunc] 239 // the result contains the `pattern' of all subpatterns that use the matching function 240 result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string { 241 g.Dump(match) 242 match[0] = "Gf" 243 return match[0] 244 }) 245 g.Dump(result) 246 g.Dump(err) 247 248 // Output: 249 // [ 250 // "Golang", 251 // "G", 252 // ] 253 // "hello Gf 2018~2021!" 254 // <nil> 255 } 256 257 func ExampleSplit() { 258 patternStr := `\d+` 259 str := "hello2020gf" 260 result := gregex.Split(patternStr, str) 261 g.Dump(result) 262 263 // Output: 264 // [ 265 // "hello", 266 // "gf", 267 // ] 268 } 269 270 func ExampleValidate() { 271 // Valid match statement 272 fmt.Println(gregex.Validate(`\d+`)) 273 // Mismatched statement 274 fmt.Println(gregex.Validate(`[a-9]\d+`)) 275 276 // Output: 277 // <nil> 278 // regexp.Compile failed for pattern "[a-9]\d+": error parsing regexp: invalid character class range: `a-9` 279 }