java替换花括号,用正则表达式替换Java中的大括号{}之间的所有文本
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) {}"
總結
以上是生活随笔為你收集整理的java替换花括号,用正则表达式替换Java中的大括号{}之间的所有文本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二级c语言需要记库函数不,【2017年必
- 下一篇: python queue模块安装_Pyt