Skip to content

Commit 41cfa48

Browse files
authored
Merge pull request #758 from travissarles/regex
Rewrote regex section of tour
2 parents c210d9a + 2fd106d commit 41cfa48

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

tutorials/tour/_posts/2017-02-13-regular-expression-patterns.md

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,50 @@ next-page: extractor-objects
1212
previous-page: xml-processing
1313
---
1414

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.
1616

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
18-
19-
```
20-
Elem(prefix:String, label:String, attrs:MetaData, scp:NamespaceBinding, children:Node*)
21-
```
17+
```tut
18+
import scala.util.matching.Regex
2219
23-
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
2521
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")
3825
}
3926
```
4027

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.
4230

43-
###General `RegExp` patterns temporarily retracted from Scala###
31+
You can also search for groups of regular expressions using parentheses.
4432

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))
49+
println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}")
50+
```
51+
Here we parse out the keys and values of a String. Each match has a group of sub-matches. Here is the output:
52+
```
53+
key: background-color value: #A03300
54+
key: background-image value: url(img
55+
key: background-position value: top center
56+
key: background-repeat value: repeat-x
57+
key: background-size value: 2160px 108px
58+
key: margin value: 0
59+
key: height value: 108px
60+
key: width value: 100
61+
```

0 commit comments

Comments
 (0)