I need to replace string with a variable inside {} using a regex
For example:
"Hi, there are {online@US-server} players in the US server" to "Hi, there are 12 players in the US server"
Inside the Strings variables inside {} can be more than 1
I need this code to allow users to modify messages. variable inside {} after @ like in the example 'US-server' are put in by users so there aren't list to check for variable. Variables could be as strange as possible ex: '{online@test-UK}' '{online@asdtest}'
public static String getReplaced(String d) {
String result = d.split("\\{online@")[0];
for (int i = 1; i < d.split("\\{online@").length; i++) {
String dd = d.split("\\{online@")[i];
Pattern p = Pattern.compile("(.*?)}(.*)");
Matcher m = p.matcher(dd);
if (m.lookingAt()) {
int count = 12;
result += count + m.group(2);
} else {
result += dd;
}
}
return result;
}