2013-10-22 35 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#findMatcher#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