Substitua todas as instâncias na cadeia de caracteres por uma substituição exclusiva
Estou tentando substituir todas as instâncias de uma String específica por uma substituição exclusiva.
O que eu gostaria:
Se eu tiver essa String:
String testScript = "while(true) { } while (10 < 7) { } while((10 < 7)) { }";
Gostaria desta saída:
while(arg0 < 5000 && true) { } while(arg1 < 5000 && 10 < 7) { } while(arg2 < 5000 && (10 < 7)) { }
O que eu tenho:
No entanto, a sequência passada parareplaceAll
não é consultado novamente (obviamente agora penso nisso).
while(arg0 < 5000 && true) { } while(arg0 < 5000 && 10 < 7) { } while(arg0 < 5000 && (10 < 7)){ }
Quaisquer respostas ou comentários, como sempre, são muito apreciados.
SSCCE:
public static void main(String[] args) {
int counter = 0;
String testScript = "while(true) { } while (10 < 7) { } while((10 < 7)) { }";
String out = testScript.replaceAll("while\\s*\\(", "while(arg" + (counter++) + " < 5000 && ");
System.out.println(out);
}