I have the following expression:
"3 + 2 * ((1 + 1 - (2 + 4))) + 112 * 31 - ((1+1) - 14 + 1)"
I want to split the expression with the top level parentesis
For example:
String Expression = "3 + 2 * ((1 + 1 - (2 + 4))) + 112 * 31 - ((1+1) - 14 + 1)";
String[] result = Regex.Split(expression, "??");
Expected output:
//result[0] = 3 + 2 *
//result[1] = (1 + 1 - (2 + 4))
//result[2] = + 112 * 31 -
//result[3] = 3 + 2 *
//result[4] = (1+1) - 14 + 1
This regex does what you want since you are using .NET. It uses a feature unique to .NET called balancing groups.
The following code:
Results in the following values populating the results array:
Here is a relevant blog post about balancing groups: http://blog.stevenlevithan.com/archives/balancing-groups