简体   繁体   English

使用java regex从String中删除括号

[英]Remove parenthesis from String using java regex

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正则表达式删除括号但我遇到错误No group 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);
}

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). 正确地处理平衡需要解析(或者仅与有限深度匹配的真正丑陋的RE,或者具有重复的正则表达式替换的“聪明”)。 For most cases, such complexity is overkill; 在大多数情况下,这种复杂性是过度的; the simplest thing that could possibly work is good enough. 可能有效的最简单的事情是足够好的。

What you want is this: s = s.replaceAll("[()]",""); 你想要的是: s = s.replaceAll("[()]","");

For more on regex, visit regex tutorial . 有关正则表达式的更多信息,请访问正则表达式教程

您收到错误是因为您的正则表达式没有任何组,但我建议您使用这种更简单的单行方法:

expr = expr.replaceAll("\\((.+?)\\)", "$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. 浏览递归下降ezpresssion解析器,Dijkstra shunting-yard算法等。

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. 正则表达式定义了一个由任何空格字符组成的字符类(\\ s,因为我们传入一个字符串而被转义为\\ s,因为我们传入一个字符串),一个短划线(因为短划线意味着在字符类的上下文中有特殊的东西而被转义),和括号。 Try it working code. 试试工作代码。

phoneNumber.replaceAll("[\\s\\-()]", "");

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 . 另外,只有在需要regex时才使用replaceAll In other cases, replace works just fine. 在其他情况下, replace工作正常。 See below: 见下文:

String s = "(4+5)+6";
String s1=s.replace("(", "").replace(")","");

Output: 输出:

4+5+6 4 + 5 + 6

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM