You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/tour/_posts/2017-02-13-regular-expression-patterns.md
+39-25Lines changed: 39 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -12,36 +12,50 @@ next-page: extractor-objects
12
12
previous-page: xml-processing
13
13
---
14
14
15
-
## Right-ignoring sequence patterns##
15
+
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.
16
16
17
-
Right-ignoring patterns are a useful feature to decompose any data which is either a subtype of `Seq[A]` or a case class with an iterated formal parameter, like for instance
In those cases, Scala allows patterns having a wildcard-star `_*` in the rightmost position to stand for arbitrary long sequences.
24
-
The following example demostrate a pattern match which matches a prefix of a sequence and binds the rest to the variable `rest`.
20
+
val numberPattern: Regex = "[0-9]".r
25
21
26
-
```tut
27
-
object RegExpTest1 extends App {
28
-
def containsScala(x: String): Boolean = {
29
-
val z: Seq[Char] = x
30
-
z match {
31
-
case Seq('s','c','a','l','a', rest @ _*) =>
32
-
println("rest is "+rest)
33
-
true
34
-
case Seq(_*) =>
35
-
false
36
-
}
37
-
}
22
+
numberPattern.findFirstMatchIn("awesomepassword") match {
23
+
case Some(_) => println("Password OK")
24
+
case None => println("Password must contain a number")
38
25
}
39
26
```
40
27
41
-
In contrast to previous Scala versions, it is no longer allowed to have arbitrary regular expressions, for the reasons described below.
28
+
In the above example, the `numberPattern` is a `Regex`
29
+
(regular expression) which we use to make sure a password contains a number.
42
30
43
-
###General `RegExp` patterns temporarily retracted from Scala###
31
+
You can also search for groups of regular expressions using parentheses.
44
32
45
-
Since we discovered a problem in correctness, this feature is temporarily retracted from the Scala language. If there is request from the user community, we might reactivate it in an improved form.
46
-
47
-
According to our opinion regular expressions patterns were not so useful for XML processing as we estimated. In real life XML processing applications, XPath seems a far better option. When we discovered that our translation or regular expressions patterns has some bugs for esoteric patterns which are unusual yet hard to exclude, we chose it would be time to simplify the language.
33
+
```tut
34
+
import scala.util.matching.Regex
35
+
36
+
val keyValPattern: Regex = "([0-9a-zA-Z-#() ]+): ([0-9a-zA-Z-#() ]+)".r
37
+
38
+
val input: String =
39
+
"""background-color: #A03300;
40
+
|background-image: url(img/header100.png);
41
+
|background-position: top center;
42
+
|background-repeat: repeat-x;
43
+
|background-size: 2160px 108px;
44
+
|margin: 0;
45
+
|height: 108px;
46
+
|width: 100%;""".stripMargin
47
+
48
+
for (patternMatch <- keyValPattern.findAllMatchIn(input))
0 commit comments