Lau*_*ura 0 java regex eclipse loops
我从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 at index 3.
I found the text "foo" starting at index 4 and ending at index 7.
I found the text "foo" starting at index 8 and ending at index 11.
I found the text "foo" starting at index 0 and ending at index 3.
Run Code Online (Sandbox Code Playgroud)
等等等,直到我终止
我究竟做错了什么?
谢谢.
没关系...>.<出于某种原因,我没有看到外面的无限循环.我一直认为这是问题的另一个循环.