Skip to content

Commit 652f36d

Browse files
authored
Merge pull request #3118 from Friendseeker/patch-1
Add documentation for Pattern Matching on `String`
2 parents 59db4e1 + 6b3d1f2 commit 652f36d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

_tour/pattern-matching.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,69 @@ println(showNotification(someVoiceRecording)) // prints You received a Voice Re
138138

139139
The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(sender, title, _)` the fields `sender` and `title` are used in the return value but the `body` field is ignored with `_`.
140140

141+
## Matching on string
142+
143+
The `s`-interpolator allows embedding variables in strings and is also useful for pattern matching.
144+
145+
{% tabs s-interpolator-pattern-matching class=tabs-scala-version %}
146+
{% tab 'Scala 2' for=s-interpolator-pattern-matching %}
147+
```scala
148+
val input: String = "Alice is 25 years old"
149+
150+
input match {
151+
case s"$name is $age years old" => s"$name's age is $age"
152+
case _ => "No match"
153+
}
154+
// Result: "Alice's age is 25"
155+
```
156+
{% endtab %}
157+
{% tab 'Scala 3' for=s-interpolator-pattern-matching %}
158+
```scala
159+
val input: String = "Alice is 25 years old"
160+
161+
input match
162+
case s"$name is $age years old" => s"$name's age is $age"
163+
case _ => "No match"
164+
// Result: "Alice's age is 25"
165+
```
166+
{% endtab %}
167+
{% endtabs %}
168+
169+
In this example, name and age extract parts of the string based on the pattern. This is helpful for parsing structured text.
170+
171+
We can also use extractor objects for string pattern matching.
172+
173+
{% tabs s-interpolator-pattern-matching-2 class=tabs-scala-version %}
174+
{% tab 'Scala 2' for=s-interpolator-pattern-matching-2 %}
175+
```scala
176+
object Age {
177+
def unapply(s: String): Option[Int] = s.toIntOption
178+
}
179+
180+
val input: String = "Alice is 25 years old"
181+
182+
val (name, age) = input match {
183+
case s"$name is ${Age(age)} years old" => (name, age)
184+
}
185+
// name: String = Alice
186+
// age: Int = 25
187+
```
188+
{% endtab %}
189+
{% tab 'Scala 3' for=s-interpolator-pattern-matching-2 %}
190+
```scala
191+
object Age:
192+
def unapply(s: String): Option[Int] = s.toIntOption
193+
194+
val input: String = "Alice is 25 years old"
195+
196+
val (name, age) = input match
197+
case s"$name is ${Age(age)} years old" => (name, age)
198+
// name: String = Alice
199+
// age: Int = 25
200+
```
201+
{% endtab %}
202+
{% endtabs %}
203+
141204
## Pattern guards
142205
Pattern guards are boolean expressions which are used to make cases more specific. Just add `if <boolean expression>` after the pattern.
143206

0 commit comments

Comments
 (0)