0

I am working on parentheses position validation in a string expression using C#. The expression looks like below.

"Loan no = 12345 AND Borrower First Name Contains Milena OR Buy Side Lock Date = 03/13/2016 AND Last Finished Milestone = Decisioned OR Subject Property State = NC AND Loan Amt > 50000"

Here 'AND' and 'OR' are the two different operations that can be performed between two conditions. We have API in C# to evaluate two conditions containing an operation.

With these things in place, the end users are allowed to place the parentheses from UI between two or more conditions in order to evaluate those conditions on priority basis. However UI allows them to put parentheses only at the beginning or end of a condition.

Let's say in the above example, if the user want to evaluate the second and third condition on priority basis he/she put the parentheses between them as shown below.

Loan no = 12345 AND (Borrower First Name Contains Milena OR Buy Side Lock Date = 03/13/2016) AND Last Finished Milestone = Decisioned OR Subject Property State = NC AND Loan Amt > 50000.

However the end user can put as many parentheses as he/she wants. Now in the above expression with parentheses in place between second and third conditions if the user tries to put the parentheses between first and second conditions the logic breaks as the second and third conditions are already in parentheses which needs to be evaluated first. In this case I want to prevent the user from putting the parentheses between first and second conditions.

I checked few articles in the net which helps me in identifying the number of left parentheses count are equal to number of right parentheses count. However that will not help me in identifying the above problem.

Does any one has any suggestions?

  • Possible duplicate of [How to evaluate custom parenthesis expression in C#?](http://stackoverflow.com/questions/36787560/how-to-evaluate-custom-parenthesis-expression-in-c) – SamWhan May 20 '16 at 07:17
  • How is this any different from your previous two questions? – Damien_The_Unbeliever May 20 '16 at 07:20
  • @ClasG, That question was posted by myself yesterday but it is on hold with the lack of explanation. So I had to post this again with more clarity in it. – user3796454 May 20 '16 at 07:25
  • 4
    So edit the question and it'll be released if you fix it. Don't ask a new one. – SamWhan May 20 '16 at 07:27

1 Answers1

0

Hope you are finding this

string expression = "[{()}]";
        List<char> myList = new List<char>();

        foreach (var item in expression.ToCharArray())
        {
            if( item == '(' || item == '{' || item == '[' )
            {
                myList.Add(item);
            }
            else if( item == ')' || item == '}' || item == ']' )
            {
                char topItem = myList[myList.Count - 1];

                if (item == '}' && topItem == '{')
                    ;
                else if (item == ']' && topItem == '[')
                    ;
                else if (item == ')' && topItem == '(')
                    ;
                else
                    // Your Error Code Here

                myList.RemoveAt(myList.Count - 1);

            }

        }
Numan Ali
  • 223
  • 2
  • 13
  • Thanks for the response. This will only help in counting left and right parentheses. This will not validate whether I have put the parentheses at the right place. And my expression will include only '(' and ')' parentheses. – user3796454 May 20 '16 at 07:42