WEB_Programming/Pure Java
Regular Expression > Test Harness
neokido
2008. 11. 6. 11:27
RegexTestHarness.java
이름을 가진 프로그램이다. API를 이용하여 정규식을 생성할 수있다. java RegexTestHarness 명령어를 이용하여 실행하면 되며, 커맨드라인 아규먼트는 받을 필요가 없다. 에플리케이션은 반복적으로 사용자에게 정규식과 입력 문자를 받아들이는 작업을 한다. 이 test harness는 옵션으로 이용이 가능하다. 그러나 다음 페이지에 나오는 논제들을 테스트 해 볼때 매우 편리함을 알게 될 것이다.import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexTestHarness {
public static void main(String[] args){
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern =
Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher =
pattern.matcher(console.readLine("Enter input string to search: "));
boolean found = false;
while (matcher.find()) {
console.format("I found the text \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group(), matcher.start(), matcher.end());
found = true;
}
if(!found){
console.format("No match found.%n");
}
}
}
}
다음 섹션으로 넘어가기 전에 이 코드를 저장하고 컴파일 하기 바란다. 환경에 맞는 패키지를 제공하여 수행되도록 해보자.