20

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);
}
6
  • In order to capture a group you need to have a pair of unescaped parentheses in your regex. However, assuming you do make this change, the rest of the code just puts the parentheses back in again, no? Commented Mar 31, 2013 at 6:19
  • Why do you need regex for this? Commented Mar 31, 2013 at 6:44
  • @KennethK. I need it for a formula parsing Commented Mar 31, 2013 at 7:38
  • You could use Jep or any other math parsing library for Java. Commented Mar 31, 2013 at 8:01
  • OK, but again I ask: Why do you need a regex to simply replace one or more parentheses? String.replace can do this job quite nicely. Commented Mar 31, 2013 at 8:14

6 Answers 6

42

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.

Sign up to request clarification or add additional context in comments.

Comments

10

What you want is this: s = s.replaceAll("[()]","");

For more on regex, visit regex tutorial.

Comments

4

You're getting the error because your regex doesn't have any groups, but I suggest you use this much simpler, one-line approach:

expr = expr.replaceAll("\\((.+?)\\)", "$1");

Comments

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.

4 Comments

My parentheses aren't nested, But Thank you for answer
@Ehsan If you're in total control of the input, as you must be to be able to assert that, I don't understand why you need an expression analyser at all. In any case my comment stands. You need a parser for expressions. Ad-hoc methods will not work. You're doing this the wrong way. The problem will resurface when you come to handle operator precedence. Do it right. There is really no alternative.
Yes, in any case your comment parsing my way is stands, and I never say that in formula parsing my way is correct, But only I face with a error on regex and I ask question for solving it
@Ehsan The solution remains the same. Tackle the whole problem the right way instead of the wrong way. Sticking your head in the sand like an ostrich is not an option. And if you're not expecting nested parentheses, why is your code recursive?
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.

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.