数字的正则表达式匹配抛出MatchError

时间:2013-05-26 04:05:08

标签: regex scala scala-2.10

我试图打破使用正则表达式的小时格式的时间字符串,但它失败并给出MatchError。 这里发生了什么?使用Scala 2.10。

scala> val minsecs = """\d+:\d+""".r
minsecs: scala.util.matching.Regex = \d+:\d+

scala> val minsecs(m,s) = "03:45"
scala.MatchError: 03:45 (of class java.lang.String)
        at .<init>(<console>:8)
        at .<clinit>(<console>)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:731)
        at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:980)
        at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:570)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:601)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
        at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
        at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
        at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
        at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
        at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
        at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
        at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
        at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
        at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
        at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:73)
        at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:64)
        at sbt.Console.console0$1(Console.scala:23)
        at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
        at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
        at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

1 个答案:

答案 0 :(得分:3)

您需要使用两个捕获组才能提取值:

val minsecs = """(\d+):(\d+)""".r
val minsecs(m,s) = "03:45"

请注意\d+周围添加的括号。