模式与Scala中的正则表达式匹配

时间:2014-11-18 12:59:55

标签: regex scala pattern-matching

为什么这不起作用?

val isGovt = """Govt .*""".r
val Govt = "Govt 23 foobar"
Govt match {
    case isGovt(_) => println("match works")
    case _ => print("nope. doesn't work")
}

打印' nope。没有工作'。 我做错了什么?

1 个答案:

答案 0 :(得分:6)

更改

val isGovt = """Govt .*""".r

val isGovt = """(Govt .*)""".r

当您使用正则表达式作为提取器时,绑定变量对应于正则表达式组。你的正则表达式没有。

您也可以按原样保留正则表达式并执行:

case isGovt() =>

这可能更像是你想到的帽子。

相关问题