小编Lau*_*ura的帖子

while(Matcher.find())无限循环

我从Oracle的Java教程中修改了以下代码:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexTestHarness {

public static void main(String[] args){

    while (true) {
        Pattern pattern = Pattern.compile("foo");
        Matcher matcher = pattern.matcher("foo foo foo");

        boolean found = false;
        while (matcher.find()) {
            System.out.format("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.format("No match found.%n");
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试学习如何在Java中使用正则表达式.(我对正则表达式非常有信心,只是没有Java的类使用它们.)我正在使用Eclipse,我也不是非常熟悉.我无法弄清楚如何让控制台不被初始化为null(正如教程警告的那样),所以我删除了它,我只是使用静态值并重新编译每次我想尝试新的东西.

当我运行此代码时,我得到一个无限循环:

I found the text "foo" starting at index 0 and ending …
Run Code Online (Sandbox Code Playgroud)

java regex eclipse loops

0
推荐指数
2
解决办法
3845
查看次数

标签 统计

eclipse ×1

java ×1

loops ×1

regex ×1