2013-10-22 1 views
2

나는이 정규식을 perl에서 사용할 수 있습니다. 그러나 자바에서는이 코드를 실행할 때 예외가 발생합니다.Java 정규식이 일치하지 않음 - perl에서 OK

String procTime="125-23:02:01"; 
    String pattern = "([0-9]+)-([0-9]+):([0-9]+):([0-9]+).*"; 
    Pattern r = Pattern.compile(pattern); 
    Matcher mt = r.matcher(procTime); 
    String a = mt.group(0); // throws exception not fnd 
    String d = mt.group(1); 

답변

5

당신은 당신의 코드에서 Matcher#find 또는 Matcher#matches 명령을 호출 아닙니다. 다음 작업을 수행 할 수 있습니다.

String procTime="125-23:02:01"; 
String pattern = "([0-9]+)-([0-9]+):([0-9]+):([0-9]+).*"; 
Pattern r = Pattern.compile(pattern); 
Matcher mt = r.matcher(procTime); 
if (mt.find()) { 
    String a = mt.group(0); // should work now 
    String d = mt.group(1); 
} 
+1

감사합니다. (주석의 길이는 15 자 이상이어야합니다) – jessarah

관련 문제