upstreamnet
2008-12-18 06:33:26 UTC
Hi,
I am trying to learn groovy by running some simple scripts, and I ran across
some odd behavior with
regex pattern matching that I'm hoping someone might be able to explain to
me.
In the program below I will get IllegalStateException : No match found
UNLESS I uncomment the
statement
println matcher[0]
I was hoping someone might point me to a good explanation of why regex
matching works this way...
It seems counter-intuitive -- I would think if I ask for the first group
(group 1) that was generated
from the match, I should just get that group ("oovy is a dyn") without
having to access the matcher's
contents via the bracket operator. But if I don't use the bracket
operator, I get the exception.
THE SIMPLE REGEX PROGRAM
def text = "Groovy is a dynamic language"
//assert text =~ /ua/ // true because there is a match
matcher = text =~ /(oo.*dyn).*ua(.)e/
//assert matcher instanceof java.util.regex.Matcher
println " groupCount is " + matcher.groupCount();
println " count is " + matcher.getCount();
if (matcher.getCount()) {
//println matcher[0] // REMOVE THE COMMENT TO
MAKE THE EXCEPTION GO AWAY.
println " group 0 is " + matcher.group(0);
println " group 1 is " + matcher.group(1);
println " group 2 is " + matcher.group(2);
}
I am trying to learn groovy by running some simple scripts, and I ran across
some odd behavior with
regex pattern matching that I'm hoping someone might be able to explain to
me.
In the program below I will get IllegalStateException : No match found
UNLESS I uncomment the
statement
println matcher[0]
I was hoping someone might point me to a good explanation of why regex
matching works this way...
It seems counter-intuitive -- I would think if I ask for the first group
(group 1) that was generated
from the match, I should just get that group ("oovy is a dyn") without
having to access the matcher's
contents via the bracket operator. But if I don't use the bracket
operator, I get the exception.
THE SIMPLE REGEX PROGRAM
def text = "Groovy is a dynamic language"
//assert text =~ /ua/ // true because there is a match
matcher = text =~ /(oo.*dyn).*ua(.)e/
//assert matcher instanceof java.util.regex.Matcher
println " groupCount is " + matcher.groupCount();
println " count is " + matcher.getCount();
if (matcher.getCount()) {
//println matcher[0] // REMOVE THE COMMENT TO
MAKE THE EXCEPTION GO AWAY.
println " group 0 is " + matcher.group(0);
println " group 1 is " + matcher.group(1);
println " group 2 is " + matcher.group(2);
}
--
View this message in context: http://www.nabble.com/strange-regex-behavior%3A-getting-%27no-match-found%27-when-i-try-to-exec-matcher.group%280%29%2C-unless-I-access-matcher-as-a-list-first.-tp21067708p21067708.html
Sent from the groovy - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
View this message in context: http://www.nabble.com/strange-regex-behavior%3A-getting-%27no-match-found%27-when-i-try-to-exec-matcher.group%280%29%2C-unless-I-access-matcher-as-a-list-first.-tp21067708p21067708.html
Sent from the groovy - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email