groovy regex match group

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

groovy regex match group

Vladimir Frelikh
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!
MG
Reply | Threaded
Open this post in threaded view
|

Re: groovy regex match group

MG
you have to call matches() on the matcher...

-------- Ursprüngliche Nachricht --------
Von: Vladimir Frelikh <[hidden email]>
Datum: 14.03.18 12:17 (GMT+01:00)
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!
MG
Reply | Threaded
Open this post in threaded view
|

Re: groovy regex match group

MG
In reply to this post by Vladimir Frelikh
ps: if(matcher) invokes Groovy truth (asBoolean()), which calls find() on the matcher

-------- Ursprüngliche Nachricht --------
Von: Vladimir Frelikh <[hidden email]>
Datum: 14.03.18 12:17 (GMT+01:00)
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!