Debian Codegraph @

example_test.go

raw symbols SCIP
1 package re2_test
2
3 import (
4 "fmt"
5 "strings"
6
7 regexp "github.com/betterleaks/go-re2"
8 )
9
10 func Example() {
11 // Compile the expression once, usually at init time.
12 // Use raw strings to avoid having to quote the backslashes.
13 validID := regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)
14
15 fmt.Println(validID.MatchString("adam[23]"))
16 fmt.Println(validID.MatchString("eve[7]"))
17 fmt.Println(validID.MatchString("Job[48]"))
18 fmt.Println(validID.MatchString("snakey"))
19 // Output:
20 // true
21 // true
22 // false
23 // false
24 }
25
26 func ExampleMatch() {
27 matched, err := regexp.Match(`foo.*`, []byte(`seafood`))
28 fmt.Println(matched, err)
29 matched, err = regexp.Match(`bar.*`, []byte(`seafood`))
30 fmt.Println(matched, err)
31 matched, err = regexp.Match(`a(b`, []byte(`seafood`))
32 fmt.Println(matched, err)
33
34 // Output:
35 // true <nil>
36 // false <nil>
37 // false error parsing regexp: missing closing ): `a(b`
38 }
39
40 func ExampleMatchString() {
41 matched, err := regexp.MatchString(`foo.*`, "seafood")
42 fmt.Println(matched, err)
43 matched, err = regexp.MatchString(`bar.*`, "seafood")
44 fmt.Println(matched, err)
45 matched, err = regexp.MatchString(`a(b`, "seafood")
46 fmt.Println(matched, err)
47 // Output:
48 // true <nil>
49 // false <nil>
50 // false error parsing regexp: missing closing ): `a(b`
51 }
52
53 func ExampleQuoteMeta() {
54 fmt.Println(regexp.QuoteMeta(`Escaping symbols like: .+*?()|[]{}^$`))
55 // Output:
56 // Escaping symbols like: \.\+\*\?\(\)\|\[\]\{\}\^\$
57 }
58
59 func ExampleRegexp_Find() {
60 re := regexp.MustCompile(`foo.?`)
61 fmt.Printf("%q\n", re.Find([]byte(`seafood fool`)))
62
63 // Output:
64 // "food"
65 }
66
67 func ExampleRegexp_FindAll() {
68 re := regexp.MustCompile(`foo.?`)
69 fmt.Printf("%q\n", re.FindAll([]byte(`seafood fool`), -1))
70
71 // Output:
72 // ["food" "fool"]
73 }
74
75 func ExampleRegexp_FindAllSubmatch() {
76 re := regexp.MustCompile(`foo(.?)`)
77 fmt.Printf("%q\n", re.FindAllSubmatch([]byte(`seafood fool`), -1))
78
79 // Output:
80 // [["food" "d"] ["fool" "l"]]
81 }
82
83 func ExampleRegexp_FindSubmatch() {
84 re := regexp.MustCompile(`foo(.?)`)
85 fmt.Printf("%q\n", re.FindSubmatch([]byte(`seafood fool`)))
86
87 // Output:
88 // ["food" "d"]
89 }
90
91 func ExampleRegexp_Match() {
92 re := regexp.MustCompile(`foo.?`)
93 fmt.Println(re.Match([]byte(`seafood fool`)))
94 fmt.Println(re.Match([]byte(`something else`)))
95
96 // Output:
97 // true
98 // false
99 }
100
101 func ExampleRegexp_FindString() {
102 re := regexp.MustCompile(`foo.?`)
103 fmt.Printf("%q\n", re.FindString("seafood fool"))
104 fmt.Printf("%q\n", re.FindString("meat"))
105 // Output:
106 // "food"
107 // ""
108 }
109
110 func ExampleRegexp_FindStringIndex() {
111 re := regexp.MustCompile(`ab?`)
112 fmt.Println(re.FindStringIndex("tablett"))
113 fmt.Println(re.FindStringIndex("foo") == nil)
114 // Output:
115 // [1 3]
116 // true
117 }
118
119 func ExampleRegexp_FindStringSubmatch() {
120 re := regexp.MustCompile(`a(x*)b(y|z)c`)
121 fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-"))
122 fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-"))
123 // Output:
124 // ["axxxbyc" "xxx" "y"]
125 // ["abzc" "" "z"]
126 }
127
128 func ExampleRegexp_FindAllString() {
129 re := regexp.MustCompile(`a.`)
130 fmt.Println(re.FindAllString("paranormal", -1))
131 fmt.Println(re.FindAllString("paranormal", 2))
132 fmt.Println(re.FindAllString("graal", -1))
133 fmt.Println(re.FindAllString("none", -1))
134 // Output:
135 // [ar an al]
136 // [ar an]
137 // [aa]
138 // []
139 }
140
141 func ExampleRegexp_FindAllStringSubmatch() {
142 re := regexp.MustCompile(`a(x*)b`)
143 fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-", -1))
144 fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-", -1))
145 fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-axb-", -1))
146 fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-ab-", -1))
147 // Output:
148 // [["ab" ""]]
149 // [["axxb" "xx"]]
150 // [["ab" ""] ["axb" "x"]]
151 // [["axxb" "xx"] ["ab" ""]]
152 }
153
154 func ExampleRegexp_FindAllStringSubmatchIndex() {
155 re := regexp.MustCompile(`a(x*)b`)
156 // Indices:
157 // 01234567 012345678
158 // -ab-axb- -axxb-ab-
159 fmt.Println(re.FindAllStringSubmatchIndex("-ab-", -1))
160 fmt.Println(re.FindAllStringSubmatchIndex("-axxb-", -1))
161 fmt.Println(re.FindAllStringSubmatchIndex("-ab-axb-", -1))
162 fmt.Println(re.FindAllStringSubmatchIndex("-axxb-ab-", -1))
163 fmt.Println(re.FindAllStringSubmatchIndex("-foo-", -1))
164 // Output:
165 // [[1 3 2 2]]
166 // [[1 5 2 4]]
167 // [[1 3 2 2] [4 7 5 6]]
168 // [[1 5 2 4] [6 8 7 7]]
169 // []
170 }
171
172 func ExampleRegexp_FindSubmatchIndex() {
173 re := regexp.MustCompile(`a(x*)b`)
174 // Indices:
175 // 01234567 012345678
176 // -ab-axb- -axxb-ab-
177 fmt.Println(re.FindSubmatchIndex([]byte("-ab-")))
178 fmt.Println(re.FindSubmatchIndex([]byte("-axxb-")))
179 fmt.Println(re.FindSubmatchIndex([]byte("-ab-axb-")))
180 fmt.Println(re.FindSubmatchIndex([]byte("-axxb-ab-")))
181 fmt.Println(re.FindSubmatchIndex([]byte("-foo-")))
182 // Output:
183 // [1 3 2 2]
184 // [1 5 2 4]
185 // [1 3 2 2]
186 // [1 5 2 4]
187 // []
188 }
189
190 func ExampleRegexp_Longest() {
191 re := regexp.MustCompile(`a(|b)`)
192 fmt.Println(re.FindString("ab"))
193 re.Longest()
194 fmt.Println(re.FindString("ab"))
195 // Output:
196 // a
197 // ab
198 }
199
200 func ExampleRegexp_MatchString() {
201 re := regexp.MustCompile(`(gopher){2}`)
202 fmt.Println(re.MatchString("gopher"))
203 fmt.Println(re.MatchString("gophergopher"))
204 fmt.Println(re.MatchString("gophergophergopher"))
205 // Output:
206 // false
207 // true
208 // true
209 }
210
211 func ExampleRegexp_NumSubexp() {
212 re0 := regexp.MustCompile(`a.`)
213 fmt.Printf("%d\n", re0.NumSubexp())
214
215 re := regexp.MustCompile(`(.*)((a)b)(.*)a`)
216 fmt.Println(re.NumSubexp())
217 // Output:
218 // 0
219 // 4
220 }
221
222 func ExampleRegexp_ReplaceAll() {
223 re := regexp.MustCompile(`a(x*)b`)
224 fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("T")))
225 fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1")))
226 fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W")))
227 fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))
228 // Output:
229 // -T-T-
230 // --xx-
231 // ---
232 // -W-xxW-
233 }
234
235 func ExampleRegexp_ReplaceAllLiteralString() {
236 re := regexp.MustCompile(`a(x*)b`)
237 fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))
238 fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "$1"))
239 fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "${1}"))
240 // Output:
241 // -T-T-
242 // -$1-$1-
243 // -${1}-${1}-
244 }
245
246 func ExampleRegexp_ReplaceAllString() {
247 re := regexp.MustCompile(`a(x*)b`)
248 fmt.Println(re.ReplaceAllString("-ab-axxb-", "T"))
249 fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
250 fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
251 fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))
252 // Output:
253 // -T-T-
254 // --xx-
255 // ---
256 // -W-xxW-
257 }
258
259 func ExampleRegexp_ReplaceAllStringFunc() {
260 re := regexp.MustCompile(`[^aeiou]`)
261 fmt.Println(re.ReplaceAllStringFunc("seafood fool", strings.ToUpper))
262 // Output:
263 // SeaFooD FooL
264 }
265
266 func ExampleRegexp_SubexpNames() {
267 re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`)
268 fmt.Println(re.MatchString("Alan Turing"))
269 fmt.Printf("%q\n", re.SubexpNames())
270 reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1])
271 fmt.Println(reversed)
272 fmt.Println(re.ReplaceAllString("Alan Turing", reversed))
273 // Output:
274 // true
275 // ["" "first" "last"]
276 // ${last} ${first}
277 // Turing Alan
278 }
279
280 func ExampleRegexp_SubexpIndex() {
281 re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`)
282 fmt.Println(re.MatchString("Alan Turing"))
283 matches := re.FindStringSubmatch("Alan Turing")
284 lastIndex := re.SubexpIndex("last")
285 fmt.Printf("last => %d\n", lastIndex)
286 fmt.Println(matches[lastIndex])
287 // Output:
288 // true
289 // last => 2
290 // Turing
291 }
292
293 func ExampleRegexp_Split() {
294 a := regexp.MustCompile(`a`)
295 fmt.Println(a.Split("banana", -1))
296 fmt.Println(a.Split("banana", 0))
297 fmt.Println(a.Split("banana", 1))
298 fmt.Println(a.Split("banana", 2))
299 zp := regexp.MustCompile(`z+`)
300 fmt.Println(zp.Split("pizza", -1))
301 fmt.Println(zp.Split("pizza", 0))
302 fmt.Println(zp.Split("pizza", 1))
303 fmt.Println(zp.Split("pizza", 2))
304 // Output:
305 // [b n n ]
306 // []
307 // [banana]
308 // [b nana]
309 // [pi a]
310 // []
311 // [pizza]
312 // [pi a]
313 }
314
315 func ExampleRegexp_Expand() {
316 content := []byte(`
317 # comment line
318 option1: value1
319 option2: value2
320 # another comment line
321 option3: value3
322 `)
323
324 // Regex pattern captures "key: value" pair from the content.
325 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)
326
327 // Template to convert "key: value" to "key=value" by
328 // referencing the values captured by the regex pattern.
329 template := []byte("$key=$value\n")
330
331 result := []byte{}
332
333 // For each match of the regex in the content.
334 for _, submatches := range pattern.FindAllSubmatchIndex(content, -1) {
335 // Apply the captured submatches to the template and append the output
336 // to the result.
337 result = pattern.Expand(result, template, content, submatches)
338 }
339 fmt.Println(string(result))
340 // Output:
341 // option1=value1
342 // option2=value2
343 // option3=value3
344 }
345
346 func ExampleRegexp_ExpandString() {
347 content := `
348 # comment line
349 option1: value1
350 option2: value2
351 # another comment line
352 option3: value3
353 `
354
355 // Regex pattern captures "key: value" pair from the content.
356 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)
357
358 // Template to convert "key: value" to "key=value" by
359 // referencing the values captured by the regex pattern.
360 template := "$key=$value\n"
361
362 result := []byte{}
363
364 // For each match of the regex in the content.
365 for _, submatches := range pattern.FindAllStringSubmatchIndex(content, -1) {
366 // Apply the captured submatches to the template and append the output
367 // to the result.
368 result = pattern.ExpandString(result, template, content, submatches)
369 }
370 fmt.Println(string(result))
371 // Output:
372 // option1=value1
373 // option2=value2
374 // option3=value3
375 }
376
377 func ExampleRegexp_FindIndex() {
378 content := []byte(`
379 # comment line
380 option1: value1
381 option2: value2
382 `)
383 // Regex pattern captures "key: value" pair from the content.
384 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)
385
386 loc := pattern.FindIndex(content)
387 fmt.Println(loc)
388 fmt.Println(string(content[loc[0]:loc[1]]))
389 // Output:
390 // [18 33]
391 // option1: value1
392 }
393
394 func ExampleRegexp_FindAllSubmatchIndex() {
395 content := []byte(`
396 # comment line
397 option1: value1
398 option2: value2
399 `)
400 // Regex pattern captures "key: value" pair from the content.
401 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)
402 allIndexes := pattern.FindAllSubmatchIndex(content, -1)
403 for _, loc := range allIndexes {
404 fmt.Println(loc)
405 fmt.Println(string(content[loc[0]:loc[1]]))
406 fmt.Println(string(content[loc[2]:loc[3]]))
407 fmt.Println(string(content[loc[4]:loc[5]]))
408 }
409 // Output:
410 // [18 33 18 25 27 33]
411 // option1: value1
412 // option1
413 // value1
414 // [35 50 35 42 44 50]
415 // option2: value2
416 // option2
417 // value2
418 }
419
420 func ExampleRegexp_FindAllIndex() {
421 content := []byte("London")
422 re := regexp.MustCompile(`o.`)
423 fmt.Println(re.FindAllIndex(content, 1))
424 fmt.Println(re.FindAllIndex(content, -1))
425 // Output:
426 // [[1 3]]
427 // [[1 3] [4 6]]
428 }