开发者

Scala pattern matching against URLs

Is there a Scala library/example that will parse a URL/URI into a case class structure for pattern matching开发者_开发技巧?


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:

  • 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.


You can use java's URL which can parse an URL for its different components and is completely Scala compatible.


The following library can help you parse URIs into an instance of a case class. (Disclaimer: it is my own library) https://github.com/theon/scala-uri

You parse like so:

import com.github.theon.uri.Uri._
val uri:Uri = "http://example.com?one=1&two=2"

It provides a DSL for building URLs with query strings:

val uri = "http://example.com" ? ("one" -> 1) & ("two" -> 2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜