This one has me stumped.
I'm writing a tool which parses a lot of text and generates SQL queries & XML files. I'm having trouble with one of the methods. From the string (just for example)
Where XXXX is VTK label: 127 16 (0 = OK and 1 = FAIL)
I am trying to extract 4 pieces of information: both bits and their respective values (0, OK, 1, FAIL). The values (OK & FAIL) can be pretty much anything--there's a lot of variance.
I'm having trouble with a particular method in my app, so I copied the
exact method into a test app (and hard coded the String s to test the method). Here's my code:
import java.util.regex.*;
public class RegexTestHarness {
static String paramLabel;
static String paramBit;
public static void main(String[] args){
String s = "Where XXXX is VTK label: 127 16 (0 = OK and 1 = FAIL)";
RegEx2(s);
}
public static void RegEx2 (String s) {
Pattern pattern = Pattern.compile("^Where.*label:\\s+\\d{1,3}\\s+\\d{1,2}\\s+\\((\\d+)\\s+=\\s+(.*)\\s+and\\s+(\\d+)\\s+=\\s+(.*)\\)");
Matcher matcher = pattern.matcher(s);
boolean found = false;
while (matcher.find()) {
String[] results = {
matcher.group(1), //exception gets thrown here, line 21
matcher.group(2),
matcher.group(3),
matcher.group(4)
};
for (String r: results) { //print results
System.out.println("Results: " + r);
}
String a = results[0];
String b = results[1];
String c = results[2];
String d = results[3];
System.out.println(a + "=" + b + "," + c + "=" + d);
found = true;
}
if(!found){
System.out.format("No match found.%n");
}
}
}
When I run this in Eclipse (_not_ in debug mode), I get exactly the output I expect:
Results: 0
Results: OK
Results: 1
Results: FAIL
0=OK,1=FAIL
Now, this is where it get's screwy. If I set a breakpoint here
String[] results = {
matcher.group(1), //Breakpoint here
or on matcher.group(2, 3 or 4), and run the
exact same code (no changes) in
debug mode, here's what happens:
1) The debugger stops on the breakpoint (of course)
2) When I resume, I get the following error:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:461)
at RegexTestHarness.RegEx2(RegexTestHarness.java:21)
at RegexTestHarness.main(RegexTestHarness.java:10)
I am running:
Eclipse 3.2.1
JDK 1.5.0_11
My questions are:
1) Is my regular expression correct? I checked it in the RegexUtil plugin for Eclipse and it seems okay, but I am pretty green still on regexes. Am I using regular expressions properly or am I doing something bassackwards? The only reason I am questioning the regex is because of the "IllegalStateException: No match found" error (in debug mode only, mind you).
2) Does anyone have any idea what may be causing this debugger behavior? A setting? A bug?
Any insight would be greatly appreciated.
Thanks!