다음 JUnit 테스트는 정규식 행에 중단 점을 설정할 때만 올바르게 실행됩니다. 정상적으로 실행하거나 중단 점없이 디버깅하면 실패합니다.왜 RegEx와 일치하는 것은 중단 점을 설정할 때만 작동합니까?
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);
}
}
}
다음 예외가 나는 정규식 행성 요리 책 here을 만들
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)
을 던져 그것을 잘 작동합니다.
그래서 @sblundy와 @bouzuya는 Matcher 객체에서'matches()'를 호출하는 것을 잊어 버렸습니다. 디버거가 matcher 객체를 평가하는 이유는 무엇입니까? – oschrenk