how to capture parenthesized groups with java regex -
i've string :
(((a * b) + c) * d) and want capture parenthesized groups java regex. thought simple regex
pattern p = pattern.compile("\\((.*)\\)",pattern.dotall); would work not.
whats wrong that?
the language you're trying define regular expression unfortunately smells non-regular, i.e. regular expressions not suitable type of expressions. (to precise, "well balanced parenthesis" not can define regular expressions.)
if want find substring a * b in example, 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"
Comments
Post a Comment