简体   繁体   中英

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.

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). 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("[()]","");

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.

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.

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

Also, use replaceAll only if you are in need of a regex . In other cases, replace works just fine. See below:

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

Output:

4+5+6

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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