-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_test.go
74 lines (66 loc) · 1.48 KB
/
re_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
73
74
/* Package re
* @Author 砚池/Ivan
* @Date 2024/04/10
* @Description:
*/
package re
import (
"regexp"
"testing"
)
func TestRe(t *testing.T) {
expression := ExpressionBuilder().
StartOfLine().
CaptureWithName("foo").
Then("a").
Dot().
EndCapture().
Build()
println(expression.String())
println(expression.regexp.MatchString("ababababababa"))
str := `aba`
t.Log(expression.GetText(str, "foo"))
}
func TestModifier(t *testing.T) {
r := regexp.MustCompile(`(?ism)CaSe`)
res := r.MatchString("case")
t.Log(res)
expression := ExpressionBuilder().
StartOfLine().
Then("CaSe").
Build()
t.Log(expression.String())
t.Log(expression.Test("CaSe"))
t.Log(expression.Test("case"))
t.Log(expression.Test("CASE"))
}
func TestGroup(t *testing.T) {
str := `abcaAabbbxxxx`
expression := ExpressionBuilder().
StartOfLine().
Anything().
CaptWithName("foo").
Then("a").Count(2).
EndCapt().
Anything().
EndOfLine().
WithAnyCase().
Build()
t.Log(expression.String())
t.Log(expression.Test(str))
t.Log(expression.GetText(str, "foo"))
t.Log(expression.GetTextGroups(str, 0))
}
func TestRegex(t *testing.T) {
//目标字符串
searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18"
//pattern := `[0-9]+\.[0-9]+` //正则表达式
expression := ExpressionBuilder().
Digit().OneOrMore().
Then("\\.").
Digit().OneOrMore().
Build()
t.Log(expression.String())
t.Log(expression.Test(searchIn))
t.Log(expression.Regexp().ReplaceAllString(searchIn, "##.#"))
}