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 = Pattern.compile("(?<=\\{).*?(?=\\})", Pattern.DOTALL).matcher(data).replaceAll("");
"if (true) { \n\t\tcalc();\n }" becomes "if (true) {}"
You need to escape the opening brace as it denotes the start of the quantifier - {n}
in regex. And you don't really need that capture groups, so remove it.
data = data.replaceAll("\\{.*?}", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With