# Find First Parentheses in String

Hey guys,
I’m currently working in an “in game” programming game. What i need is find text in the first parenthese the script found. Here’s an example: “((990 / 71) - 3^4.5) + 2 * (36.1 / 44.8 - (76.93 + 9.5))” should be returning this string “((990 / 71) - 3^4.5)”. Tell my if you don’t understand, i speak french. I have tried many possibility and here’s what i found which can be useful: First, find the first index of “(” after find the first index of “)”. If there’s an other “(” between “(” and “)”, like this: “(exemple()”, the code need to find the next index of “)”, like this “(exemple()example)”. I know this the worst question…

You should represent the equations as trees. Check out this StackOverflow question, that has a basic primer for how to think about it. It’s for Java, but the terms are pretty universal.

Hi, that should help:

``````string expression = "((990 / 71) - 3^4.5) + 2 * (36.1 / 44.8 - (76.93 + 9.5))";
int match = 0;
var finalExpression = new StringBuilder();

foreach (var character in expression.ToCharArray(0, expression.Length))
{
if (character == '(')
match++;

if (character == ')')
match--;

finalExpression.Append(character);
if (match == 0)
break;
}

Debug.log(finalExpression.ToString());
``````