Name: bsC130419 Date: 06/11/2001
java version "1.4.0-beta" Java(TM)
2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65Java HotSpot(TM)
Client VM (build 1.4.0-beta-b65, mixed mode)
1. Remove the initial '0' in the test code before the date string wrapped by
the CharBuffer, and comment out the subsequent buffer.get(). Run the code. The
result should be correct. Now add that '0' back in, leaving the buffer.get()
commented out. Run the code again. The match fails, which is also correct. Now
put the buffer.get() back in by removing the comment tags. Run the code. The
match succeeds, which is correct, but the results are shifted incorrectly by
one character. The Matcher assumes is has found the match at the start of the
buffer, as opposed to the buffers get position, which is really the case. The
Matcher needs to be aware that its CharSequence may begin feeding characters at
some position within it greater than zero, which CharBuffer can do. That
functionality is important, and the match behavior seems correct, but the
derived results are not, and any replacements made using those results may also
be effected (I didn't test that).
2.
package com.xes.test;
import java.io.*;
import java.nio.*;
import java.util.regex.*;
public class Spruce {
private static final String convertString = "^([0-9]+|[a-zA-Z]{3}\\.?|[a-zA-
Z]+)( |/|-)([0-9]+)(,? |/|-)([0-9]+)$";
public static void main (String[] args)
throws Exception {
Pattern pattern;
Matcher matcher;
CharBuffer buffer;
long a,b,c,d;
int count;
a = System.currentTimeMillis();
pattern = Pattern.compile(convertString);
b = System.currentTimeMillis();
buffer = CharBuffer.wrap("0Jan. 23, 1996");
buffer.get();
matcher = pattern.matcher(buffer);
c = System.currentTimeMillis();
matcher.matches();
d = System.currentTimeMillis();
System.out.println("Parse:" + (b - a));
System.out.println("Match:" + (d - c));
for (count = 0; count < matcher.groupCount(); count++) {
System.out.println(count + ":" + matcher.group(count));
}
}
}
3. See the output.
(Review ID: 126308)
======================================================================