@@ -13,6 +13,9 @@ redirect_from: "/tutorials/tour/regular-expression-patterns.html"
13
13
14
14
Regular expressions are strings which can be used to find patterns (or lack thereof) in data. Any string can be converted to a regular expression using the ` .r ` method.
15
15
16
+ {% tabs regex-patterns_numberPattern class=tabs-scala-version %}
17
+
18
+ {% tab 'Scala 2' for=regex-patterns_numberPattern %}
16
19
``` scala mdoc
17
20
import scala .util .matching .Regex
18
21
@@ -23,12 +26,30 @@ numberPattern.findFirstMatchIn("awesomepassword") match {
23
26
case None => println(" Password must contain a number" )
24
27
}
25
28
```
29
+ {% endtab %}
30
+
31
+ {% tab 'Scala 3' for=regex-patterns_numberPattern %}
32
+ ``` scala
33
+ import scala .util .matching .Regex
34
+
35
+ val numberPattern : Regex = " [0-9]" .r
36
+
37
+ numberPattern.findFirstMatchIn(" awesomepassword" ) match
38
+ case Some (_) => println(" Password OK" )
39
+ case None => println(" Password must contain a number" )
40
+ ```
41
+ {% endtab %}
42
+
43
+ {% endtabs %}
26
44
27
45
In the above example, the ` numberPattern ` is a ` Regex `
28
46
(regular expression) which we use to make sure a password contains a number.
29
47
30
48
You can also search for groups of regular expressions using parentheses.
31
49
50
+ {% tabs regex-patterns_keyValPattern class=tabs-scala-version %}
51
+
52
+ {% tab 'Scala 2' for=regex-patterns_keyValPattern %}
32
53
``` scala mdoc
33
54
import scala .util .matching .Regex
34
55
@@ -47,6 +68,31 @@ val input: String =
47
68
for (patternMatch <- keyValPattern.findAllMatchIn(input))
48
69
println(s " key: ${patternMatch.group(1 )} value: ${patternMatch.group(2 )}" )
49
70
```
71
+ {% endtab %}
72
+
73
+ {% tab 'Scala 3' for=regex-patterns_keyValPattern %}
74
+ ``` scala
75
+ import scala .util .matching .Regex
76
+
77
+ val keyValPattern : Regex = " ([0-9a-zA-Z- ]+): ([0-9a-zA-Z-#()/. ]+)" .r
78
+
79
+ val input : String =
80
+ """ background-color: #A03300;
81
+ |background-image: url(img/header100.png);
82
+ |background-position: top center;
83
+ |background-repeat: repeat-x;
84
+ |background-size: 2160px 108px;
85
+ |margin: 0;
86
+ |height: 108px;
87
+ |width: 100%;""" .stripMargin
88
+
89
+ for patternMatch <- keyValPattern.findAllMatchIn(input) do
90
+ println(s " key: ${patternMatch.group(1 )} value: ${patternMatch.group(2 )}" )
91
+ ```
92
+ {% endtab %}
93
+
94
+ {% endtabs %}
95
+
50
96
Here we parse out the keys and values of a String. Each match has a group of sub-matches. Here is the output:
51
97
```
52
98
key: background-color value: #A03300
@@ -61,6 +107,9 @@ key: width value: 100
61
107
62
108
Moreover, regular expressions can be used as patterns (in ` match ` expressions) to conveniently extract the matched groups:
63
109
110
+ {% tabs regex-patterns_saveContactInfomation class=tabs-scala-version %}
111
+
112
+ {% tab 'Scala 2' for=regex-patterns_saveContactInfomation %}
64
113
``` scala mdoc
65
114
def saveContactInfomation (contact : String ): Unit = {
66
115
import scala .util .matching .Regex
@@ -82,11 +131,36 @@ saveContactInfomation("123-456-7890")
82
131
saveContactInfomation(" JohnSmith@sample.domain.com" )
83
132
saveContactInfomation(" 2 Franklin St, Mars, Milky Way" )
84
133
```
134
+ {% endtab %}
135
+
136
+ {% tab 'Scala 3' for=regex-patterns_saveContactInfomation %}
137
+ ``` scala
138
+ def saveContactInfomation (contact : String ): Unit =
139
+ import scala .util .matching .Regex
140
+
141
+ val emailPattern : Regex = """ ^(\w+)@(\w+(.\w+)+)$""" .r
142
+ val phonePattern : Regex = """ ^(\d{3}-\d{3}-\d{4})$""" .r
143
+
144
+ contact match
145
+ case emailPattern(localPart, domainName, _) =>
146
+ println(s " Hi $localPart, we have saved your email address. " )
147
+ case phonePattern(phoneNumber) =>
148
+ println(s " Hi, we have saved your phone number $phoneNumber. " )
149
+ case _ =>
150
+ println(" Invalid contact information, neither an email address nor phone number." )
151
+
152
+ saveContactInfomation(" 123-456-7890" )
153
+ saveContactInfomation(" JohnSmith@sample.domain.com" )
154
+ saveContactInfomation(" 2 Franklin St, Mars, Milky Way" )
155
+ ```
156
+ {% endtab %}
157
+
158
+ {% endtabs %}
85
159
86
160
The output would be:
87
161
88
162
```
89
163
Hi, we have saved your phone number 123-456-7890.
90
164
Hi JohnSmith, we have saved your email address.
91
165
Invalid contact information, neither an email address nor phone number.
92
- ```
166
+ ```
0 commit comments