I have a piece of code that tries to repeatedly match a pattern with an incremented index inside a loop:
for(int count = 0; count < args.length; count++) {
message.replaceFirst("^\\{" + count + "\\d}$", args[count]);
}
What I want to be able to do is replace occurrences of {0}, {1} and so on in the message string with those in args. But try as I might, I simply cannot get the pattern to match. I am no regex expert, but I have tried a variety of regex combinations based on other questions here. I also tried using the replace method in StringUtils to no avail. Can anyone offer any suggestions?
Do not use a regex at all. You know your search string beforehand and it is constant (i.e. you can build it from constant strings plus variable
count) and thus there is no need to incorporate regular expressions.