gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:26:11 25 4
gpt4 key购买 nike

我从 Oracle's Java Tutorials 修改了以下代码:

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");
}
}
}
}

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

当我运行此代码时,出现无限循环:

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.

等等,等等,直到我点击终止

我做错了什么?

谢谢。

没关系...>.< 由于某种原因,我没有看到外面的无限循环。我一直认为问题出在另一个循环上。

最佳答案

当前,您的代码的该部分周围有一个 while(true)while(true) 是一个无限循环,你似乎永远无法摆脱它。

关于java - while(Matcher.find()) 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6538138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com