Inside of a class I have a pattern private Pattern lossWer = Pattern.compile("^\\d+ \\d+ (\\d+).*"). One of the functions looks like this:
public double[] getWer(){
double[] wer = new double[someStrings.size()];
Matcher m;
for(int i = 0; i < wer.length; i++){
m = lossWer.matcher(someStrings.get(i));
wer[i] = Double.parseDouble(m.group(1));
}
return wer;
}
Calling this fails with java.lang.IllegalStateException: No match found. When I change it to this, though, it works:
public double[] getWer(){
double[] wer = new double[someStrings.size()];
Matcher m;
for(int i = 0; i < wer.length; i++){
m = lossWer.matcher(someStrings.get(i));
if(!m.matches())
;
wer[i] = Double.parseDouble(m.group(1));
}
return wer;
}
Of course my application doesn’t just use a blank semi-colon for that line, but I’m illustrating that the line here does nothing but allows the program to proceed without error. Why are lines matched without errors in the second example but not in the first?
You can’t use group() without first calling either find() or matches(). That’s how regexes work. First you create a pattern, then a matcher, then you either find() instances of the regex or check if it matches().