forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regular-expressions.html
330 lines (255 loc) · 15 KB
/
regular-expressions.html
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 正则表达式</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'text-templates';
}
if (e.key == "ArrowRight") {
window.location.href = 'json';
}
}
</script>
<body>
<div class="example" id="regular-expressions">
<h2><a href="./">Go by Example 中文版</a>: 正则表达式</h2>
<table>
<tr>
<td class="docs">
<p>Go 提供了内建的<a href="http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F">正则表达式</a>支持。
这儿有一些在 Go 中与 regexp 相关的常见用法示例。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/Y7AyZ86mrxt"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"bytes"</span>
<span class="s">"fmt"</span>
<span class="s">"regexp"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>测试一个字符串是否符合一个表达式。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">match</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">regexp</span><span class="p">.</span><span class="nf">MatchString</span><span class="p">(</span><span class="s">"p([a-z]+)ch"</span><span class="p">,</span> <span class="s">"peach"</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">match</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>上面我们是直接使用字符串,但是对于一些其他的正则任务,
你需要通过 <code>Compile</code> 得到一个优化过的 <code>Regexp</code> 结构体。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">r</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">regexp</span><span class="p">.</span><span class="nf">Compile</span><span class="p">(</span><span class="s">"p([a-z]+)ch"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>该结构体有很多方法。这是一个类似于我们前面看到的匹配测试。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">MatchString</span><span class="p">(</span><span class="s">"peach"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>查找匹配的字符串。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">FindString</span><span class="p">(</span><span class="s">"peach punch"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这个也是查找首次匹配的字符串,
但是它的返回值是,匹配开始和结束位置的索引,而不是匹配的内容。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"idx:"</span><span class="p">,</span> <span class="nx">r</span><span class="p">.</span><span class="nf">FindStringIndex</span><span class="p">(</span><span class="s">"peach punch"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Submatch</code> 返回完全匹配和局部匹配的字符串。
例如,这里会返回匹配 <code>p([a-z]+)ch</code> 和 <code>([a-z]+)</code> 的信息。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">FindStringSubmatch</span><span class="p">(</span><span class="s">"peach punch"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>类似的,这个会返回完全匹配和局部匹配位置的索引。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">FindStringSubmatchIndex</span><span class="p">(</span><span class="s">"peach punch"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>带 <code>All</code> 的这些函数返回全部的匹配项,
而不仅仅是首次匹配项。例如查找匹配表达式全部的项。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">FindAllString</span><span class="p">(</span><span class="s">"peach punch pinch"</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>All</code> 同样可以对应到上面的所有函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"all:"</span><span class="p">,</span> <span class="nx">r</span><span class="p">.</span><span class="nf">FindAllStringSubmatchIndex</span><span class="p">(</span>
<span class="s">"peach punch pinch"</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这些函数接收一个非负整数作为第二个参数,来限制匹配次数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">FindAllString</span><span class="p">(</span><span class="s">"peach punch pinch"</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>上面的例子中,我们使用了字符串作为参数,
并使用了 <code>MatchString</code> 之类的方法。
我们也可以将 <code>String</code> 从函数名中去掉,并提供 <code>[]byte</code> 的参数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">Match</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="s">"peach"</span><span class="p">)))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>创建正则表达式的全局变量时,可以使用 <code>Compile</code> 的变体 <code>MustCompile</code> 。
<code>MustCompile</code> 用 <code>panic</code> 代替返回一个错误 ,这样使用全局变量更加安全。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">r</span> <span class="p">=</span> <span class="nx">regexp</span><span class="p">.</span><span class="nf">MustCompile</span><span class="p">(</span><span class="s">"p([a-z]+)ch"</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"regexp:"</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>regexp</code> 包也可以用来将子字符串替换为为其它值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nf">ReplaceAllString</span><span class="p">(</span><span class="s">"a peach"</span><span class="p">,</span> <span class="s">"<fruit>"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Func</code> 变体允许您使用给定的函数来转换匹配的文本。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">in</span> <span class="o">:=</span> <span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="s">"a peach"</span><span class="p">)</span>
<span class="nx">out</span> <span class="o">:=</span> <span class="nx">r</span><span class="p">.</span><span class="nf">ReplaceAllFunc</span><span class="p">(</span><span class="nx">in</span><span class="p">,</span> <span class="nx">bytes</span><span class="p">.</span><span class="nx">ToUpper</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">out</span><span class="p">))</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go run regular-expressions.go
<span class="go">true
</span><span class="go">true
</span><span class="go">peach
</span><span class="go">idx: [0 5]
</span><span class="go">[peach ea]
</span><span class="go">[0 5 1 3]
</span><span class="go">[peach punch pinch]
</span><span class="go">all: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]
</span><span class="go">[peach punch]
</span><span class="go">true
</span><span class="go">regexp: p([a-z]+)ch
</span><span class="go">a <fruit>
</span><span class="go">a PEACH</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>有关 Go 正则表达式的说明,请参考 <a href="http://golang.org/pkg/regexp/"><code>regexp</code></a> 包文档。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="json.html">JSON</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bytes\"\u000A \"fmt\"\u000A \"regexp\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' match, _ :\u003D regexp.MatchString(\"p([a-z]+)ch\", \"peach\")\u000A fmt.Println(match)\u000A');codeLines.push(' r, _ :\u003D regexp.Compile(\"p([a-z]+)ch\")\u000A');codeLines.push(' fmt.Println(r.MatchString(\"peach\"))\u000A');codeLines.push(' fmt.Println(r.FindString(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(\"idx:\", r.FindStringIndex(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringSubmatch(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringSubmatchIndex(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindAllString(\"peach punch pinch\", -1))\u000A');codeLines.push(' fmt.Println(\"all:\", r.FindAllStringSubmatchIndex(\u000A \"peach punch pinch\", -1))\u000A');codeLines.push(' fmt.Println(r.FindAllString(\"peach punch pinch\", 2))\u000A');codeLines.push(' fmt.Println(r.Match([]byte(\"peach\")))\u000A');codeLines.push(' r \u003D regexp.MustCompile(\"p([a-z]+)ch\")\u000A fmt.Println(\"regexp:\", r)\u000A');codeLines.push(' fmt.Println(r.ReplaceAllString(\"a peach\", \"\u003Cfruit\u003E\"))\u000A');codeLines.push(' in :\u003D []byte(\"a peach\")\u000A out :\u003D r.ReplaceAllFunc(in, bytes.ToUpper)\u000A fmt.Println(string(out))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>