开发者

Cannot trace out why this regex code runs infinite

开发者 https://www.devze.com 2023-04-11 16:31 出处:网络
I just copied from oracle.com website and made a slight c开发者_如何学编程hanges(altered console to System.out) and I compiled.

I just copied from oracle.com website and made a slight c开发者_如何学编程hanges(altered console to System.out) and I compiled.

But it runs infinitely.

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) will keep running until you break; out of it.


well, you have a while (true) { in your code - that 'might' be an infinite loop :D

0

精彩评论

暂无评论...
验证码 换一张
取 消