【问题标题】:Why scala regex not match html a href为什么scala正则表达式不匹配html a href
【发布时间】:2014-06-06 20:05:12
【问题描述】:

这是我的代码

object RegexParser { 
  def main(args :Array[String]){ 
  val html = """test<a href="/web/content/">web content</a>""" 
  val Content = ".*<a href=\"(.*)\">.*".r
  html match { 
    case Content(c) => println(s"c=${c}") 
    case _ => println("wokao") 
  } 
 } 
}

为什么 html 与 Content 不匹配?

【问题讨论】:

标签: html regex scala


【解决方案1】:

这就像一个魅力(在 Scala 2.9.2 上,所以没有 s"")。

scala> val html = """test<a href="/web/content/">web content</a>"""
html: java.lang.String = test<a href="/web/content/">web content</a>

scala> val Content = ".*<a href=\"(.*)\">.*".r
Content: scala.util.matching.Regex = .*<a href="(.*)">.*

scala> html match {
     | case Content(c) => println("c=" + c)
     | case _ => println("no match")
     | }
c=/web/content/

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多