You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Vladimir Frelikh <e2...@gmail.com> on 2018/03/14 11:17:36 UTC

groovy regex match group

Hello,
could someone help with the following code:

text = 'test'
def matcher = text =~ /(test)/
print matcher.group(1)

getting error:
java.lang.IllegalStateException: No match found

but the following code works as expected:

text = 'test'
def matcher = text =~ /(test)/
if (matcher ) {
    print matcher.group(1)
}

Thanks!

Re: groovy regex match group

Posted by mg <mg...@arscreat.com>.
ps: if(matcher) invokes Groovy truth (asBoolean()), which calls find() on the matcher
-------- Ursprüngliche Nachricht --------Von: Vladimir Frelikh <e2...@gmail.com> Datum: 14.03.18  12:17  (GMT+01:00) An: users@groovy.apache.org Betreff: groovy regex match group 
Hello,could someone help with the following code:
text = 'test'def matcher = text =~ /(test)/print matcher.group(1)
getting error:java.lang.IllegalStateException: No match found

but the following code works as expected:
text = 'test'def matcher = text =~ /(test)/if (matcher ) {    print matcher.group(1)}
Thanks!


Re: groovy regex match group

Posted by mg <mg...@arscreat.com>.
you have to call matches() on the matcher...
-------- Ursprüngliche Nachricht --------Von: Vladimir Frelikh <e2...@gmail.com> Datum: 14.03.18  12:17  (GMT+01:00) An: users@groovy.apache.org Betreff: groovy regex match group 
Hello,could someone help with the following code:
text = 'test'def matcher = text =~ /(test)/print matcher.group(1)
getting error:java.lang.IllegalStateException: No match found

but the following code works as expected:
text = 'test'def matcher = text =~ /(test)/if (matcher ) {    print matcher.group(1)}
Thanks!