Discussion:
[groovy-user] strange regex behavior: getting 'no match found' when i try to exec matcher.group(0), unless I access matcher as a list first.
upstreamnet
2008-12-18 06:33:26 UTC
Permalink
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);
}
--
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
Jim White
2008-12-18 07:29:56 UTC
Permalink
Post by upstreamnet
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]
Try:

println matcher[0][0]

That's the whole string of the first match.

The first group of the first match:

println matcher[0][1]

http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups

Jim


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
buildmaster-x
2008-12-18 20:38:41 UTC
Permalink
Thanks to Jim I was able to get understand that my original problem was
caused by
getCount() doing a reset of the match state. This reverts the matcher to
a 'no match found yet' state, so there are no groups available. when you
try to get
the value of any group 0...n you will get an exception.

I wrote a little test to confirm my understanding of what is going on, and
how to use
find and reset with a matcher.

***********************************************


import java.util.regex.Matcher

private Matcher setUpMatcher(String text) {
matcher = text =~ /(oo.*dyn).*ua(.)e/
return matcher
}

// This shows that we expect to get an error if we call the group method
without first calling find.
// We would also get the error if we called 'find' twice on an input
sequence that only had one
// occurence of a match against the regex pattern we are using.
//
private def verifyErrorOccursWhenTryingToGetGroupStateAtThisPoint(Matcher
theMatcher) {
boolean gotErr = false;
try {
println " group 0 is " + theMatcher.group(0)
} catch (IllegalStateException e) {
gotErr = true;
};
assert (gotErr)
};

private def printGroups(Matcher theMatcher) {
println "Group state"
println " group 0 is " + theMatcher.group(0)
println " group 1 is " + theMatcher.group(1);
println " group 2 is " + theMatcher.group(2)
println ""
}


// Main script

Matcher matcher = setUpMatcher("Groovy is a dynamic language")

verifyErrorOccursWhenTryingToGetGroupStateAtThisPoint(matcher)

matcher.find(); // Call find to get initialize the state of the groups
after the first match.

println (
"Now we can display the results of collecting our two groups
(oo.*dyn) and (.) after hitting the first match."
);
printGroups(matcher);

assert(matcher.find() == false); // We only have one match given our reg
ex pattern and 'text'
// So if we try to get the group state
we will find we get an exception
// because no groups were found on this
match cycle.
verifyErrorOccursWhenTryingToGetGroupStateAtThisPoint(matcher)

println "Show we can reset the matcher and then print the group state";
matcher.reset();
matcher.find();
printGroups(matcher);
--
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.-tp21067708p21080496.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
buildmaster-x
2008-12-18 20:38:44 UTC
Permalink
Thanks to Jim I was able to get understand that my original problem was
caused by
getCount() doing a reset of the match state. This reverts the matcher to
a 'no match found yet' state, so there are no groups available. when you
try to get
the value of any group 0...n you will get an exception.

I wrote a little test to confirm my understanding of what is going on, and
how to use
find and reset with a matcher.

***********************************************


import java.util.regex.Matcher

private Matcher setUpMatcher(String text) {
matcher = text =~ /(oo.*dyn).*ua(.)e/
return matcher
}

// This shows that we expect to get an error if we call the group method
without first calling find.
// We would also get the error if we called 'find' twice on an input
sequence that only had one
// occurence of a match against the regex pattern we are using.
//
private def verifyErrorOccursWhenTryingToGetGroupStateAtThisPoint(Matcher
theMatcher) {
boolean gotErr = false;
try {
println " group 0 is " + theMatcher.group(0)
} catch (IllegalStateException e) {
gotErr = true;
};
assert (gotErr)
};

private def printGroups(Matcher theMatcher) {
println "Group state"
println " group 0 is " + theMatcher.group(0)
println " group 1 is " + theMatcher.group(1);
println " group 2 is " + theMatcher.group(2)
println ""
}


// Main script

Matcher matcher = setUpMatcher("Groovy is a dynamic language")

verifyErrorOccursWhenTryingToGetGroupStateAtThisPoint(matcher)

matcher.find(); // Call find to get initialize the state of the groups
after the first match.

println (
"Now we can display the results of collecting our two groups
(oo.*dyn) and (.) after hitting the first match."
);
printGroups(matcher);

assert(matcher.find() == false); // We only have one match given our reg
ex pattern and 'text'
// So if we try to get the group state
we will find we get an exception
// because no groups were found on this
match cycle.
verifyErrorOccursWhenTryingToGetGroupStateAtThisPoint(matcher)

println "Show we can reset the matcher and then print the group state";
matcher.reset();
matcher.find();
printGroups(matcher);
--
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.-tp21067708p21080497.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
Loading...