我剛剛從oracle.com網站複製,並做了一些細微的改變(改變控制檯到System.out),我編譯。無法追查爲什麼這個正則表達式代碼運行無限
但它運行無限。
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class RegexTest {
public static void main(String[] args){
while (true) {
Pattern pattern =
Pattern.compile("int");
Matcher matcher =
pattern.matcher("int void int mathint");
boolean found = false;
while (matcher.find()) {
System.out.printf("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){
System.out.printf("No match found.%n");
}
}
}
}
或者直到拋出一個異常。 – Thomas
是的,他們也應該被抓住並相應處理。 – Kaivosukeltaja