用正则表达式替换Java中大括号{}之间的所有文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19166426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace all text between braces { } in Java with regex
提问by Jister13
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?
我收到一个错误,所以我做错了什么/我应该怎么做?
回答by Rohit Jain
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.
您需要转义大括号,因为它表示量词的开始 -{n}
在正则表达式中。而且您并不真正需要那个捕获组,因此将其删除。
data = data.replaceAll("\{.*?}", "");
回答by Cruncher
In general, this is a job that's not suited for regex to begin with. Usually if "{ text }" is possible then so is: "{ { text1 } { text2 } }" which cannot be parsed properly with regex.
一般来说,这是一项不适合开始使用正则表达式的工作。通常,如果 "{ text }" 是可能的,那么: "{ { text1 } { text2 } }" 不能用正则表达式正确解析。
This is the same reason why XML/HTML parsers do not use regex
这与 XML/HTML 解析器不使用正则表达式的原因相同
回答by Rohit Srivastava
try this, may this will help you.
试试这个,也许这会帮助你。
String refinedData = new String(data);
Pattern p = Pattern.compile("\{[^\}]*\}");
Matcher m = p.matcher(data);
while(m.find()){
String d = data.substring(m.start(), m.end());
refinedData = refinedData.replace(d, "");
}
回答by roblovelock
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) {}"
"if (true) { calc(); }" 变成 "if (true) {}"
This will replace all text between curly brackets and remove the brackets
这将替换大括号之间的所有文本并删除括号
data = data.replaceAll("\{.*?\}", "");
"if (true) { calc(); }" becomes "if (true)"
"if (true) { calc(); }" 变成 "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) {}"
"if (true) { \n\t\tcalc();\n }" 变成了 "if (true) {}"
回答by Wiktor Stribi?ew
When you need to match a substring from a single characterup to another singlecharacter, the best regex solution is to use the delimiters and insert in-between them a negated character classthat matches any character but the delimiter characters.
当您需要将一个子字符串从单个字符匹配到另一个单个字符时,最好的正则表达式解决方案是使用分隔符并在它们之间插入一个否定字符类,该类匹配除分隔符之外的任何字符。
So, the basic pseudo patternis
所以,基本的伪模式是
a [^ ab ]* b
where:
在哪里:
a
- a starting delimiter[^ab]*
- zero or more characters other thana
andb
b
- a trailing delimiter.
a
- 起始分隔符[^ab]*
- 除a
和之外的零个或多个字符b
b
- 尾随分隔符。
So, to replace all text between {
and }
(between braces), use
因此,要替换{
和}
(大括号之间)之间的所有文本,请使用
String data = "text 1 {\ntext 2\ntext 3\n}";
System.out.println(data.replaceAll("\{[^{}]*}", ""));
// => text 1
See the IDEONE demo
Note that in Java regex, {
and }
do not have to be escaped inside the character class, and }
does not have to be escaped even outside of it. Only {
outside of a character classmust be escaped (or put into a character class) so as not to form a limiting quantifier construct.
请注意,在Java中的正则表达式,{
并且}
也没有在字符类中进行转义,而}
不必进行外连的逃跑了。只有{
在字符类之外必须转义(或放入字符类),以免形成限制量词结构。