archangle
archangle

Reputation: 227

how to capture parenthesized groups with java regex

I've some string like :

(((a * b) + c) * d)

and want to capture parenthesized groups with java regex. I thought this simple regex

Pattern p = Pattern.compile("\\((.*)\\)",Pattern.DOTALL);

would do the work but it does not.

Whats wrong with that?

Upvotes: 3

Views: 3992

Answers (3)

El Ronnoco
El Ronnoco

Reputation: 11922

I believe it is virtually impossible to deal with nested structures using a RegEx. Much better to iterate through each character and keep track of how many open brackets you have.

Also, if you're aiming to evaluate a mathematical expression in infix notation you would probably have more success using the shunting algorithm.

Upvotes: 1

Skilldrick
Skilldrick

Reputation: 70819

Regexes aren't good at picking up balanced pairs like parentheses. You'd do much better to parse the string without a regex.

Upvotes: 1

aioobe
aioobe

Reputation: 420951

The language you're trying to define with your regular expression unfortunately smells non-regular, i.e. regular expressions are not suitable for this type of expressions. (To be precise, "well balanced parenthesis" is not something you can define with regular expressions.)

If you however simply want to find the substring a * b in your example, the following expressions should do:

Pattern p = Pattern.compile("\\(([^()]*)\\)");
Matcher m = p.matcher("(((a * b) * ) + c) * d)");
if (m.find())
    System.out.println(m.group(1));   // prints "a * b"

Upvotes: 3

Related Questions