无法跟踪为什么这个正则表达式代码无限运行



我只是从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");
            }
        }
    }
}

while(true)将继续运行,直到您的break;退出。

好吧,你的代码中有一个while (true) { -那'可能'是一个无限循环:D

最新更新