I want to remove parenthesis using Java regular expression but I faced to error No group 1
please see my code and help me.
我想使用Java正则表达式删除括号,但是我遇到了第1组错误,请查看我的代码并帮助我。
public String find_parenthesis(String Expr){
String s;
String ss;
Pattern p = Pattern.compile("\\(.+?\\)");
Matcher m = p.matcher(Expr);
if(m.find()){
s = m.group(1);
ss = "("+s+")";
Expr = Expr.replaceAll(ss, s);
return find_parenthesis(Expr);
}
else
return Expr;
}
and it is my main:
这是我的主要工作:
public static void main(String args[]){
Calculator c1 = new Calculator();
String s = "(4+5)+6";
System.out.println(s);
s = c1.find_parenthesis(s);
System.out.println(s);
}
6 个解决方案
#1
24
The simplest method is to just remove all parentheses from the string, regardless of whether they are balanced or not.
最简单的方法是从字符串中删除所有括号,不管它们是否平衡。
String replaced = "(4+5)+6".replaceAll("[()]", "");
Correctly handling the balancing requires parsing (or truly ugly REs that only match to a limited depth, or “cleverness” with repeated regular expression substitutions). For most cases, such complexity is overkill; the simplest thing that could possibly work is good enough.
正确处理平衡需要解析(或真正丑陋的,只匹配有限的深度,或“聪明”的重复正则表达式替换)。在大多数情况下,这种复杂性是多余的;最简单的可能起作用的东西也足够好了。
#2
9
What you want is this: s = s.replaceAll("[()]","");
您想要的是:s = s.replaceAll("[() ",");
For more on regex, visit regex tutorial.
有关regex的更多信息,请访问regex教程。
#3
3
You're getting the error because your regex doesn't have any groups, but I suggest you use this much simpler, one-line approach:
您会得到错误,因为您的regex没有任何组,但我建议您使用更简单的一行方法:
expr = expr.replaceAll("\\((.+?)\\)", "$1");
#4
1
You can't do this with a regex at all. It won't remove the matching parentheses, just the first left and the first right, and then you won't be able to get the correct result from the expression. You need a parser for expressions. Have a look around for recursive descent ezpresssion parsers, the Dijkstra shunting-yard algorithm, etc.
使用regex根本不能这样做。它不会删除匹配的括号,只删除第一个左括号和第一个右括号,然后您将无法从表达式中得到正确的结果。表达式需要一个解析器。看看周围的递归下降ezpresssion解析器,Dijkstra分流算法等。
#5
1
The regular expression defines a character class consisting of any whitespace character (\s, which is escaped as \s because we're passing in a String), a dash (escaped because a dash means something special in the context of character classes), and parentheses. Try it working code.
正则表达式定义了一个由任何空格字符组成的字符类(\s,因为我们传入了一个字符串而被转义为\s)、一个破折号(转义为在字符类上下文中有特殊含义)和圆括号。试一试工作代码。
phoneNumber.replaceAll("[\\s\\-()]", "");
#6
-1
I know I'm very late here. But, just in case you're still looking for a better answer. If you want to remove both open and close parenthesis from a string, you can use a very simple method like this:
我知道我来晚了。但是,以防你还在寻找更好的答案。如果您想从字符串中同时删除开括号和闭括号,您可以使用一个非常简单的方法:
String s = "(4+5)+6";
s=s.replaceAll("\\(", "").replaceAll("\\)","");
If you are using this:
如果你使用这个:
s=s.replaceAll("()", "");
you are instructing the code to look for ()
which is not present in your string. Instead you should try to remove the parenthesis separately.
您正在指示要查找()的代码,该代码不在您的字符串中。相反,您应该尝试分别删除括号。
To explain in detail, consider the below code:
要详细解释,请考虑以下代码:
String s = "(4+5)+6";
String s1=s.replaceAll("\\(", "").replaceAll("\\)","");
System.out.println(s1);
String s2 = s.replaceAll("()", "");
System.out.println(s2);
The output for this code will be:
本代码输出如下:
4+5+6
4 + 5 + 6
(4+5)+6
(4 + 5 + 6)
Also, use replaceAll
only if you are in need of a regex
. In other cases, replace
works just fine. See below:
此外,只有在需要regex时才使用replaceAll。在其他情况下,替换就可以了。见下文:
String s = "(4+5)+6";
String s1=s.replace("(", "").replace(")","");
Output:
输出:
4+5+6
4 + 5 + 6
Hope this helps!
希望这可以帮助!
#1
24
The simplest method is to just remove all parentheses from the string, regardless of whether they are balanced or not.
最简单的方法是从字符串中删除所有括号,不管它们是否平衡。
String replaced = "(4+5)+6".replaceAll("[()]", "");
Correctly handling the balancing requires parsing (or truly ugly REs that only match to a limited depth, or “cleverness” with repeated regular expression substitutions). For most cases, such complexity is overkill; the simplest thing that could possibly work is good enough.
正确处理平衡需要解析(或真正丑陋的,只匹配有限的深度,或“聪明”的重复正则表达式替换)。在大多数情况下,这种复杂性是多余的;最简单的可能起作用的东西也足够好了。
#2
9
What you want is this: s = s.replaceAll("[()]","");
您想要的是:s = s.replaceAll("[() ",");
For more on regex, visit regex tutorial.
有关regex的更多信息,请访问regex教程。
#3
3
You're getting the error because your regex doesn't have any groups, but I suggest you use this much simpler, one-line approach:
您会得到错误,因为您的regex没有任何组,但我建议您使用更简单的一行方法:
expr = expr.replaceAll("\\((.+?)\\)", "$1");
#4
1
You can't do this with a regex at all. It won't remove the matching parentheses, just the first left and the first right, and then you won't be able to get the correct result from the expression. You need a parser for expressions. Have a look around for recursive descent ezpresssion parsers, the Dijkstra shunting-yard algorithm, etc.
使用regex根本不能这样做。它不会删除匹配的括号,只删除第一个左括号和第一个右括号,然后您将无法从表达式中得到正确的结果。表达式需要一个解析器。看看周围的递归下降ezpresssion解析器,Dijkstra分流算法等。
#5
1
The regular expression defines a character class consisting of any whitespace character (\s, which is escaped as \s because we're passing in a String), a dash (escaped because a dash means something special in the context of character classes), and parentheses. Try it working code.
正则表达式定义了一个由任何空格字符组成的字符类(\s,因为我们传入了一个字符串而被转义为\s)、一个破折号(转义为在字符类上下文中有特殊含义)和圆括号。试一试工作代码。
phoneNumber.replaceAll("[\\s\\-()]", "");
#6
-1
I know I'm very late here. But, just in case you're still looking for a better answer. If you want to remove both open and close parenthesis from a string, you can use a very simple method like this:
我知道我来晚了。但是,以防你还在寻找更好的答案。如果您想从字符串中同时删除开括号和闭括号,您可以使用一个非常简单的方法:
String s = "(4+5)+6";
s=s.replaceAll("\\(", "").replaceAll("\\)","");
If you are using this:
如果你使用这个:
s=s.replaceAll("()", "");
you are instructing the code to look for ()
which is not present in your string. Instead you should try to remove the parenthesis separately.
您正在指示要查找()的代码,该代码不在您的字符串中。相反,您应该尝试分别删除括号。
To explain in detail, consider the below code:
要详细解释,请考虑以下代码:
String s = "(4+5)+6";
String s1=s.replaceAll("\\(", "").replaceAll("\\)","");
System.out.println(s1);
String s2 = s.replaceAll("()", "");
System.out.println(s2);
The output for this code will be:
本代码输出如下:
4+5+6
4 + 5 + 6
(4+5)+6
(4 + 5 + 6)
Also, use replaceAll
only if you are in need of a regex
. In other cases, replace
works just fine. See below:
此外,只有在需要regex时才使用replaceAll。在其他情况下,替换就可以了。见下文:
String s = "(4+5)+6";
String s1=s.replace("(", "").replace(")","");
Output:
输出:
4+5+6
4 + 5 + 6
Hope this helps!
希望这可以帮助!