我有以下循环:模式匹配替换周期在一个无限循环
public class Main {
public static void main (String[] args){
String test = "#{value} lorem ipsum #{value} lorem ipsum";
String regex = "(#\\{)([^}]*)(})";
Pattern callPattern = Pattern.compile(regex);
Matcher callMatcher = callPattern.matcher(test);
while (callMatcher.find()) {
test = callMatcher.replaceFirst(generate());
}
System.out.println(test);
}
private static String generate(){
Random random = new Random();
return String.valueOf(random.nextInt(100));
}
}
和执行陷在我的while循环。过去我使用过类似的算法,那为什么这个卡住了?它似乎能够取代第一次出现,但后来发现但从未取代第二次。
谢谢。有用。现在我想知道其他类似的周期是如何工作的。我在同一个项目中至少有另一个。 – EBM