I need to get all expression between parenthesis in a mathematical operation, with scala.
I tried to do it with a regex. And it works with expressions like:
(2+4)
-> Result: 2+4
4*(3+1)
-> Result: 3+1
But It's impossible for me to get all values, as in the following example:
(2+1)*(4-3)
-> Result: 2+1)*(4+3
Expected result:
`2+1`
`4+3`
Where "formula" is the input expression
val regex = Pattern.compile("\\((.*)\\)")
val regexMatcher = regex.matcher(formula)
while (regexMatcher.find()) {
println(regexMatcher.group(1)); //Fetching Group from String
}
EDIT: In case of (1+(2+3))
, the good result would be 1+(2+3)