-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
72 lines (65 loc) · 1.62 KB
/
string_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package regexer_test
import (
"fmt"
"strings"
"github.com/orsinium-labs/regexer"
)
func ExampleString_Find() {
rex := regexer.New(`[a-z]+`)
input := "never gonna give you up"
matches := rex.String(input).Find()
for match := range matches {
fmt.Println(match.Span.Start, match.Content)
}
//Output:
// 0 never
// 6 gonna
// 12 give
// 17 you
// 21 up
}
func ExampleString_Contains() {
rex := regexer.New(`[0-9]+`)
input := "number 42 is the answer"
contains := rex.String(input).Contains()
if contains {
fmt.Println("the string contains a regexp match")
}
//Output: the string contains a regexp match
}
func ExampleString_Replace() {
rex := regexer.New(`(is|the)`)
input := "number 42 is the answer"
var result string
matches := rex.String(input).Replace(&result)
for match := range matches {
newVal := strings.ToUpper(match.Content)
match.ReplaceLiteral(newVal)
}
fmt.Println(result)
//Output: number 42 IS THE answer
}
func ExampleSReplacement_ReplaceLiteral() {
rex := regexer.New(`(is|the)`)
input := "number 42 is the answer"
var result string
matches := rex.String(input).Replace(&result)
for match := range matches {
newVal := strings.ToUpper(match.Content)
match.ReplaceLiteral(newVal)
}
fmt.Println(string(result))
//Output: number 42 IS THE answer
}
func ExampleSReplacement_ReplaceTemplate() {
rex := regexer.New(`(is|the)`)
input := "number 42 is the answer"
var result string
matches := rex.String(input).Replace(&result)
for match := range matches {
template := string(`[$1]`)
match.ReplaceTemplate(template)
}
fmt.Println(string(result))
//Output: number 42 [is] [the] answer
}