Scala 模式匹配 URL [英] Scala pattern matching against URLs

查看:53
本文介绍了Scala 模式匹配 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 Scala 库/示例可以将 URL/URI 解析为 case 类结构以进行模式匹配?

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

推荐答案

这是一个提取器,可以为您从 URL 中提取某些部分:

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
}

这篇关于Scala 模式匹配 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆