Is there a Scala library/example that will parse a URL/URI into a case class structure for pattern matching?
Delta Lake with Apache Spark using ScalaA pattern match includes a sequence of alternatives, each starting with the keyword case. Each alternative includes a pattern and one or more expressions, which will be evaluated if the pattern matches. An arrow symbol => separates the pattern from the expressions.
Notes. Scala's pattern matching statement is most useful for matching on algebraic types expressed via case classes. Scala also allows the definition of patterns independently of case classes, using unapply methods in extractor objects.
Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C.
case _ => does not check for the type, so it would match anything (similar to default in Java). case _ : ByteType matches only an instance of ByteType . It is the same like case x : ByteType , just without binding the casted matched object to a name x .
Here's an extractor that will get some parts out of a URL for you:
object UrlyBurd {
def unapply(in: java.net.URL) = Some((
in.getProtocol,
in.getHost,
in.getPort,
in.getPath
))
}
val u = new java.net.URL("http://www.google.com/")
u match {
case UrlyBurd(protocol, host, port, path) =>
protocol +
"://" + host +
(if (port == -1) "" else ":" + port) +
path
}
I would suggest to use the facility provided by extractors for regular expressions.
For instance:
val URL = """(http|ftp)://(.*)\.([a-z]+)""".r
def splitURL(url : String) = url match {
case URL(protocol, domain, tld) => println((protocol, domain, tld))
}
splitURL("http://www.google.com") // prints (http,www.google,com)
Some explanations:
.r
method on strings (actually, on StringLike
s) turns them into an instance of Regex
.Regex
es define an unapplySeq
method, which allows them to be used as extractors in pattern-matching (note that you have to give them a name that starts with a capital letter for this to work).(...)
in the regular expression.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With