What is the Groovy equivalent of the following Perl code?
my $txt = "abc : groovy : def";
if ($txt =~ / : (.+?) : /) {
my $match = $1;
print "MATCH=$match\n";
# should print "MATCH=groovy\n"
}
I know that there's more than one way to do it (including the regular Java way) - but what is the "Groovy way" of doing it?
This is one way of doing it, but it feels a bit clumsy - especially the array notation (m[0][1]
) which feels a bit strange. Is there a better way do it? If not - please describe the logic behind m[0][1]
.
def txt = "java : groovy : grails"
if ((m = txt =~ / : (.+?) :/)) {
def match = m[0][1]
println "MATCH=$match"
}
This was the closest match to the Perl code that I could achieve:
def txt = "abc : groovy : def"
if ((m = txt =~ / : (.+?) : /)) {
def match = m.group(1)
println "MATCH=$match"
}
// Prints:
// MATCH=groovy