Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala pattern matching against URLs

Tags:

scala

Is there a Scala library/example that will parse a URL/URI into a case class structure for pattern matching?

like image 858
Eric Hauser Avatar asked Sep 28 '11 16:09

Eric Hauser


People also ask

How do you match a pattern in Scala?

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.

Does Scala have pattern matching?

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.

What is the use of Scala pattern matching?

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.

What is case _ in Scala?

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 .


2 Answers

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
}
like image 148
Alex Cruise Avatar answered Sep 22 '22 21:09

Alex Cruise


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:

  • The .r method on strings (actually, on StringLikes) turns them into an instance of Regex.
  • Regexes 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).
  • The values that are going to be passed into the binders you use in the pattern are defined by the groups (...) in the regular expression.
like image 43
Philippe Avatar answered Sep 22 '22 21:09

Philippe