Name: bsC130419 Date: 05/31/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
A successful match using the matches() or lookingAt()
method doesn't always update the start and end positions
of the overall match (group 0). The result, as shown in
the code sample below, is that a call to matcher.group()
returns null--which, as I understand it, should never
happen. The find() method always seems to update the
positions properly.
Looking at the code, I see that find() works by calling
pattern.root.match(), while the other two methods call
pattern.matchRoot.match(). Furthermore, the matchRoot
object object starts out as an instance of LastNode, and
in LastNode's match() method, the lines that set the
overall start and end positions are commented out.
-------------------------- PatternTest.java -----------------------
import java.util.regex.*;
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
A successful match using the matches() or lookingAt()
method doesn't always update the start and end positions
of the overall match (group 0). The result, as shown in
the code sample below, is that a call to matcher.group()
returns null--which, as I understand it, should never
happen. The find() method always seems to update the
positions properly.
Looking at the code, I see that find() works by calling
pattern.root.match(), while the other two methods call
pattern.matchRoot.match(). Furthermore, the matchRoot
object object starts out as an instance of LastNode, and
in LastNode's match() method, the lines that set the
overall start and end positions are commented out.
-------------------------- PatternTest.java -----------------------
import java.util.regex.*;
public class PatternTest
{
public static void main(String[] argv)
{
Pattern pattern = Pattern.compile("(tes)ting");
Matcher matcher = pattern.matcher("testing");
if (matcher.find()) {
System.out.println("\nfind:");
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
}
pattern = Pattern.compile("te(sti)ng");
matcher = pattern.matcher("testing");
if (matcher.lookingAt()) {
System.out.println("\nlookingAt:");
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
}
pattern = Pattern.compile("test(ing)");
matcher = pattern.matcher("testing");
if (matcher.matches()) {
System.out.println("\nmatches:");
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
}
pattern = Pattern.compile("te(sti)ng!");
matcher = pattern.matcher("testing");
if (matcher.lookingAt()) {
System.out.println("\nlookingAt with bang:");
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
}
pattern = Pattern.compile("^test(ing)");
matcher = pattern.matcher("testing");
if (matcher.matches()) {
System.out.println("\nmatches with caret:");
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
}
}
}
------------------------------ output ---------------------------------
find:
testing
tes
lookingAt:
null
sti
matches:
null
ing
lookingAt with bang:
testing
sti
matches with caret:
testing
ing
(Review ID: 125324)
======================================================================