scala正则表达式获取url的host

小编今天要分享的是,通过正则表达式获取url中的host,
在实际开发中这是很常用的,大家收好了。
代码:

import java.util.regex.Pattern

/**
 * Created by Administrator on 2017/9/26.
 */
object UrlGeyHostTest {
  def main(args: Array[String]): Unit = {
    //传参
    val url1 = "http://tieba.baidu.com/p/4336698825"
    val url2 = "http://mp.weixin.qq.com/s?__biz=MzIyODgyNDk0OQ==&mid=2247483988&idx=3&sn=7181bbef257e27014051272d785eeafd&scene=4#wechat_redirect"
    var host = ""
    val p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+")
    val matcher = p.matcher(url2)
    if (matcher.find()) {
      host = matcher.group()
    }
    println(host)
  }

}

ok
分享完毕

你可能感兴趣的:(Java编程,scala)