File tree Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
layout : tour
3
- title : 正则表达式
3
+ title : 正则表达式模式
4
4
5
5
discourse : false
6
6
@@ -14,7 +14,8 @@ next-page: extractor-objects
14
14
previous-page : singleton-objects
15
15
---
16
16
17
- 正则表达式是一类特殊的字符串,可以用来在数据中寻找特定模式。 任何字符串都可以通过调用 ` .r ` 方法来转换成正则表达式。
17
+ 正则表达式是用来找出数据中的指定模式(或缺少该模式)的字符串。` .r ` 方法可使任意字符串变成一个正则表达式。
18
+
18
19
19
20
``` tut
20
21
import scala.util.matching.Regex
@@ -27,9 +28,9 @@ numberPattern.findFirstMatchIn("awesomepassword") match {
27
28
}
28
29
```
29
30
30
- 在上例中,常量 ` numberPattern ` 是一个 ` Regex ` 类型的实例(正则表达式),这里用来确保密码中含有一个数字 。
31
+ 上例中, ` numberPattern ` 的类型是正则表达式类 ` Regex ` ,其作用是确保密码中包含一个数字 。
31
32
32
- 你也可以用圆括号括起多个正则表达式,以便一次寻找多个值 。
33
+ 你还可以使用括号来同时匹配多组正则表达式 。
33
34
34
35
``` tut
35
36
import scala.util.matching.Regex
@@ -49,7 +50,8 @@ val input: String =
49
50
for (patternMatch <- keyValPattern.findAllMatchIn(input))
50
51
println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}")
51
52
```
52
- 此例中我们从字符串中解析出多组键值对。每一个匹配值都包含一组子匹配值。结果如下:
53
+ 上例解析出了一个字符串中的多个键和值,其中的每个匹配又有一组子匹配,结果如下:
54
+
53
55
```
54
56
key: background-color value: #A03300
55
57
key: background-image value: url(img
You can’t perform that action at this time.
0 commit comments