I have a long string with numerous occurences of text between { } that I would like to remove however when I do this:
data = data.replaceAll("{(.*?)}", "");
i get an error, so what am I doing wrong / how should I go about doing this?
解决方案
This will replace all text between curly brackets and leave the brackets
This is done using positive look ahead and positive look behind
data = data.replaceAll("(?<=\\{).*?(?=\\})", "");
"if (true) { calc(); }" becomes "if (true) {}"
This will replace all text between curly brackets and remove the brackets
data = data.replaceAll("\\{.*?\\}", "");
"if (true) { calc(); }" becomes "if (true)"
This will replace all text between curly brackets, including new lines.
data = pile("(?<=\\{).*?(?=\\})", Pattern.DOTALL).matcher(data).replaceAll("");
"if (true) { \n\t\tcalc();\n }" becomes "if (true) {}"