Given a String, I know Groovy provides convenience methods like
String.findAll(String, Closure)
Finds all occurrences of a regular
expression string within a String. Any
matches are passed to the specified
closure. The closure is expected to
have the full match in the first
parameter. If there are any capture
groups, they will be placed in
subsequent parameters.
However, I am looking for a similar method where the closure receives either the Matcher object or the int offset of the match. Is there such a beast?
Or, if not: is there a common way to return the offsets of all matches for a given String or Pattern as a Collection or Array of Integers / ints? (Commons / Lang or Guava are both OK, but I’d prefer plain Groovy).
I don’t know of anything that currently exists, but you could add the method to the metaClass of String if you wanted… Something like:
Which can be called by:
and returns (in this case)
Edit
Actually…a version which can work with regular expression parameters would be:
Which can then be called like:
to give:
Edit 2
And finally this code as a Category