【发布时间】:2012-01-14 01:43:35
【问题描述】:
只有当我在匹配行上设置断点时,以下 JUnit 测试才会正确运行。正常运行或者无断点调试都会失败。
public class ParserTest {
@Test
public void test() {
final String s = new Parser().parse("Hello(WORLD)");
}
public static class Parser {
private static final Pattern pattern = Pattern
.compile("([a-zA-Z\\s]*)\\(([A-Z]{5,5})\\)");
public String parse(final String raw) {
// put a breakpoint on the matcher in eclipse,
// debug as junit test, then step over
final Matcher matcher = pattern.matcher(raw);
return matcher.group(2) + ":" + matcher.group(1);
}
}
}
抛出以下异常
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:461)
at lab.ParserTest$Parser.parse(ParserTest.java:22)
at lab.ParserTest.test(ParserTest.java:11)
我创建了一个 RegEx Planet Cookbook here,它运行良好。
【问题讨论】:
-
正如@sblundy 和@bouzuya 指出的那样,我忘记在
Matcher对象上调用matches()。问题仍然存在,为什么调试器会评估匹配器对象。
标签: regex eclipse debugging junit breakpoints