how to capture parenthesized groups with java regex
I've some string like :
(((a * b) + c) * d)
and want to capture parenthesized gro开发者_C百科ups 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?
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"
Regexes aren't good at picking up balanced pairs like parentheses. You'd do much better to parse the string without a regex.
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.
精彩评论