The simple string matcher.
Attempts to match the input string to the given interpolated patterns via a naive globbing, that is the reverse of the simple interpolator.
Here is an example usage:
val s"Hello, $name" = "Hello, James"
println(name) // "James"
In this example, the string "James" ends up matching the location where the pattern $name
is positioned, and thus ends up bound to that variable.
Multiple matches are supported:
val s"$greeting, $name" = "Hello, James"
println(greeting) // "Hello"
println(name) // "James"
And the s
matcher can match an arbitrary pattern within the ${}
block, for example:
val TimeSplitter = "([0-9]+)[.:]([0-9]+)".r
val s"The time is ${TimeSplitter(hours, mins)}" = "The time is 10.50"
println(hours) // 10
println(mins) // 50
Here, we use the TimeSplitter
regex within the s
matcher, further splitting the matched string "10.50" into its constituent parts