You must use capture groups. You can check groovy documentation about it:
http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
For example, you can use this code:
s = "This is a simple string 234 something else here as well 4334" regex = /([0-9]{3})/ matcher = ( s=~ regex ) if (matcher.matches()) { println(matcher.getCount()+ " occurrence of the regular expression was found in the string."); println(matcher[0][1] + " found!") }
As a note:
m[0] is the first match object. m[0][0] is everything that matched in this match. m[0][1] is the first capture in this match. m[0][n] is the n capture in this match.
Federico piazza
source share