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