使用正则表达式解析具有三个数字组的行

时间:2013-11-21 20:43:29

标签: regex scala

我想解析git shortstat行。 E.g。

val in  = """ 6 files changed, 55 insertions(+), 1 deletions(-)"""
val out = (6, 55, 1)

如何从out获取in?我试过regexp,但可能搞错了:

import util.Try

val Ex = """ (\d+) files changed, (\d+) insertions\(+\), (\d+) deletions\(-\)""".r

def parse(line: String): Option[(Int, Int, Int)] = line match {
  case Ex(a, b, c) => (for {
      ai <- Try(a.toInt)
      bi <- Try(b.toInt)
      ci <- Try(c.toInt)
    }
    yield (ai, bi, ci)).toOption

  case _ => None
}

但是出了点问题:parse(in) == None ......

1 个答案:

答案 0 :(得分:0)

需要逃避+

//                                                  !!
val Ex = """ (\d+) files changed, (\d+) insertions\(\+\), (\d+) deletions\(-\)""".r