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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
| func main() {
PkgFunc()
Compiles()
FindContent()
FindIndex()
FindMatch()
Match()
Replace()
Expand()
Prefix()
Longest()
}
// 1. 包函数
func PkgFunc() {
// Match:匹配字节数组
fmt.Println(regexp.Match("H.* ", []byte("Hello World!")))
// MatchString:匹配字符串
fmt.Println(regexp.MatchString("H.* ", "Hello World!"))
// MatchReader:实现了io.RuneReader(io.Reader)接口
r := bytes.NewReader([]byte("Hello World!"))
fmt.Println(regexp.MatchReader("H.* ", r))
//QuoteMeta:将字符串 s 中的“特殊字符”转换为其“转义格式”
fmt.Println(regexp.QuoteMeta("(?P:Hello) [a-z]"))
}
// 2. 构建正则对象
func Compiles() {
reg, err := regexp.Compile(`\w+`)
fmt.Printf("%q,%v\n", reg.FindString("Hello World!"), err)
// 将正则表达式限制为POSIX ERE(egrep)语法,并将匹配语义更改为最左最长
// 同时,它采用最左最长方式搜索,而Compile采用最左最短方式搜索
// POSIX 语法不支持 Perl 的语法格式:\d、\D、\s、\S、\w、\W
reg, err = regexp.CompilePOSIX(`[[:word:]]+`)
fmt.Printf("%q,%v\n", reg.FindString("Hello World!"), err)
reg = regexp.MustCompile(`\w+`)
fmt.Printf("%q\n", reg.FindString("Hello World!"))
reg = regexp.MustCompilePOSIX(`[[:word:]]+`)
fmt.Printf("%q\n", reg.FindString("Hello World"))
}
// 3. 查找匹配内容
func FindContent() {
reg := regexp.MustCompile(`\w+`)
// 查找第一个匹配项
fmt.Printf("%q\n", reg.Find([]byte("Hello World!")))
fmt.Printf("%q\n", reg.FindString("Hello World!"))
// 查找所有匹配项
fmt.Printf("%q\n", reg.FindAll([]byte("Hello World!"), -1))
fmt.Printf("%q\n", reg.FindAllString("Hello World!", -1))
}
// 4. 查找起止索引位
func FindIndex() {
reg := regexp.MustCompile(`\w+`)
// 查找第一个匹配项的起止位
fmt.Println(reg.FindIndex([]byte("Hello World!")))
fmt.Println(reg.FindStringIndex("Hello World!"))
r := bytes.NewReader([]byte("Hello World!"))
fmt.Println(reg.FindReaderIndex(r))
// 查找所有匹配项的起止位
fmt.Println(reg.FindAllIndex([]byte("Hello World!"), -1))
fmt.Println(reg.FindAllStringIndex("Hello World!", -1))
}
// 5. 分组匹配(即{完整匹配项, 子匹配项, 子匹配项, ...})
func FindMatch() {
reg := regexp.MustCompile(`(\w)(\w)+`) //两个分组匹配
// 查找匹配项和子匹配项内容
fmt.Printf("%q\n", reg.FindSubmatch([]byte("Hello World!")))
fmt.Printf("%q\n", reg.FindStringSubmatch("Hello World!"))
fmt.Printf("%q\n", reg.FindAllSubmatch([]byte("Hello World!"), -1))
fmt.Printf("%q\n", reg.FindAllStringSubmatch("Hello World!", -1))
// 查找匹配项和子匹配项的起止索引位
fmt.Println(reg.FindSubmatchIndex([]byte("Hello World!")))
fmt.Println(reg.FindAllStringSubmatchIndex("Hello World!", -1))
//...
}
// 6. 正则校验
func Match() {
// Math
s := []byte(`Hello World`)
reg := regexp.MustCompile(`Hello[\w\s]+`)
fmt.Println(reg.Match(s)) //true
// MatchReader
reg = regexp.MustCompile(`Hello\w+`)
r := bytes.NewReader([]byte(`Hello World`))
fmt.Println(reg.MatchReader(r)) // false
// MatchString
reg = regexp.MustCompile(`Hello[\w\s]+`)
fmt.Println(reg.MatchString("Hello World")) //true
}
// 7. 正则替换
func Replace() {
// 将【源数据】替换成【模板格式】的数据
reg := regexp.MustCompile(`(Hell|G)o`)
src := []byte("Hello World, 123 Go!")
rep := []byte("${1}ooo")
// 打印匹配结果
fmt.Println(reg.FindAllStringSubmatch(string(src), -1))
//[[Hello Hell] [Go G]]
// 1. ReplaceAll
fmt.Printf("%q\n", reg.ReplaceAll(src, rep))
// "Hellooo World, 123 Gooo!"
// 2. ReplaceAllString
fmt.Printf("%q\n", reg.ReplaceAllString(string(src), string(rep)))
// "Hellooo World, 123 Gooo!"
// 3. ReplaceAllLiteral 直接替换(不解析模板)
fmt.Printf("%q\n", reg.ReplaceAllLiteral(src, rep))
// "${1}ooo World, 123 ${1}ooo!"
// 4. ReplaceAllLiteralString
fmt.Printf("%q\n", reg.ReplaceAllLiteralString(string(src), string(rep)))
// "${1}ooo World, 123 ${1}ooo!"
// 5. ReplaceAll
reg = regexp.MustCompile("(H)ello") //Hello H
src = []byte("Hello World!")
rep = []byte("$0$1")
fmt.Printf("%s\n", reg.ReplaceAll(src, rep))
// HelloH World!
// 6. ReplaceAllFunc
fmt.Printf("%s\n", reg.ReplaceAllFunc(src,
func(b []byte) []byte {
rst := []byte{}
rst = append(rst, b...)
rst = append(rst, "$1"...)
return rst
}))
// Hello$1 World!
//7. ReplaceAllString
fmt.Printf("%s\n", reg.ReplaceAllString("Hello World!", "$0$1"))
// HelloH World!
// 8. ReplaceAllStringFunc
fmt.Printf("%s\n", reg.ReplaceAllStringFunc("Hello World!",
func(b string) string {
return b + "$1"
}))
// Hello$1 World!
}
// 8. 正则追加(将模板追加到目标文本中)
func Expand() {
reg := regexp.MustCompile(`(\w+),(\w+)`)
src := []byte("Golang,World!") // 源文本
dst := []byte("Say: ") // 目标文本
template := []byte("Hello $1, Hello $2") // 模板
match := reg.FindSubmatchIndex(src) // 解析源文本
fmt.Printf("%q\n", reg.Expand(dst, template, src, match))
// "Say: Hello Golang, Hello World"
}
// 9. 前缀
func Prefix() {
// LiteralPrefix 返回所有匹配项都共同拥有的前缀(去除可变元素)
// prefix:共同拥有的前缀
// complete:如果 prefix 就是正则表达式本身,则返回 true,否则返回 false
reg := regexp.MustCompile(`Hello[\w\s]+`)
fmt.Println(reg.LiteralPrefix()) // Hello false
reg = regexp.MustCompile(`Hello`)
fmt.Println(reg.LiteralPrefix()) // Hello true
}
// 10. 贪婪模式
func Longest() {
text := `Hello World, 123 Go!`
pattern := `(?U)H[\w\s]+o` // 正则标记“非贪婪模式”(?U)
reg := regexp.MustCompile(pattern)
fmt.Printf("%q\n", reg.FindString(text))
// Hello
reg.Longest() // 切换到“贪婪模式”
fmt.Printf("%q\n", reg.FindString(text))
// Hello Wo
}
|
评论