如何在Groovy中解析文本

时间:2012-11-26 14:42:28

标签: parsing text groovy functional-programming

我需要解析一个文本(从svn命令输出)以便检索一个数字(svn修订版)。 这是我的代码。请注意,我需要将所有输出流检索为文本以执行其他操作。

def proc = cmdLine.execute()                 // Call *execute* on the strin
proc.waitFor()                               // Wait for the command to finish
def output = proc.in.text

//其他事情发生在这里

output.eachLine {
    line ->
    def revisionPrefix = "Last Changed Rev: "
    if (line.startsWith(revisionPrefix)) res = new Integer(line.substring(revisionPrefix.length()).trim())
}

这段代码运行正常,但由于我还是Groovy的新手,我想知道是否有更好的惯用方法可以避免丑陋的... ...

svn输出的示例(但当然问题更为一般)

Path: .
Working Copy Root Path: /svn
URL: svn+ssh://svn.company.com/opt/svnserve/repos/project/trunk
Repository Root: svn+ssh://svn.company.com/opt/svnserve/repos
Repository UUID: 516c549e-805d-4d3d-bafa-98aea39579ae
Revision: 25447
Node Kind: directory
Schedule: normal
Last Changed Author: ubi
Last Changed Rev: 25362
Last Changed Date: 2012-11-22 10:27:00 +0000 (Thu, 22 Nov 2012)

我从下面的答案中获得灵感,并使用find()解决了问题。我的解决方案是:

def revisionPrefix = "Last Changed Rev: "
def line = output.readLines().find { line -> line.startsWith(revisionPrefix) }
def res = new Integer(line?.substring(revisionPrefix.length())?.trim()?:"0")

3行,没有,如果,非常干净

1 个答案:

答案 0 :(得分:1)

一种可能的选择是:

def output = cmdLine.execute().text
Integer res = output.readLines().findResult { line ->
  (line =~ /^Last Changed Rev: (\d+)$/).with { m ->
    if( m.matches() ) {
      m[ 0 ][ 1 ] as Integer
    }
  }
}

不确定它是否更好。我相信其他人会有不同的选择

编辑:

另外,请注意使用proc.text。如果你的proc输出很多东西,那么你可能会在输入流满了时结束阻塞......

以下是使用consumeProcessOutput

的评论很多的备选方案
// Run the command
String output = cmdLine.execute().with { proc ->

  // Then, with a StringWriter
  new StringWriter().with { sw ->

    // Consume the output of the process
    proc.consumeProcessOutput( sw, System.err )

    // Make sure we worked
    assert proc.waitFor() == 0

    // Return the output (goes into `output` var)
    sw.toString()
  }
}

// Extract the version from by looking through all the lines
Integer version = output.readLines().findResult { line ->

  // Pass the line through a regular expression
  (line =~ /Last Changed Rev: (\d+)/).with { m ->

    // And if it matches
    if( m.matches() ) {

      // Return the \d+ part as an Integer
      m[ 0 ][ 1 ] as Integer
    }
  }
}