How to parse text in Groovy <p>I need to parse a text (output from a svn command) in order to retrieve a number (svn revision). This is my code. Note that I need to retrieve all the output stream as a text to do other operations.</p> <pre><code>def proc = cmdLine.execute() // Call *execute* on the strin proc.waitFor() // Wait for the command to finish def output = proc.in.text </code></pre> <p>//other stuff happening here</p> <pre><code>output.eachLine { line -&gt; def revisionPrefix = "Last Changed Rev: " if (line.startsWith(revisionPrefix)) res = new Integer(line.substring(revisionPrefix.length()).trim()) } </code></pre> <p>This code is working fine, but since I'm still a novice in Groovy, I'm wondering if there were a better idiomatic way to avoid the ugly if...</p> <p>Example of svn output (but of course the problem is more general)</p> <pre><code>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) </code></pre> <p>I've got inspiration from the answer below and I solved using find(). My solution is:</p> <pre><code>def revisionPrefix = "Last Changed Rev: " def line = output.readLines().find { line -&gt; line.startsWith(revisionPrefix) } def res = new Integer(line?.substring(revisionPrefix.length())?.trim()?:"0") </code></pre> <p>3 lines, no if, very clean</p>