Scala pattern matching against URLs

2020-02-27 05:48发布

问题:

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

回答1:

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
}


回答2:

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.


回答3:

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



回答4:

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)


标签: scala