Scala Language Extractors Regex Extractors


A regular expression with grouped parts can be used as an extractor:

scala> val address = """(.+):(\d+)""".r
address: scala.util.matching.Regex = (.+):(\d+)

scala> val address(host, port) = ""
host: String =
port: String = 8080

Note that when it is not matched, a MatchError will be thrown at runtime:

scala> val address(host, port) = "something not a host and port"
scala.MatchError: something not a host and port (of class java.lang.String)