Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Does any one has any suggestions?

What I have tried:

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.
Posted
Updated 20-May-16 2:08am
v2

1 solution

Regular expressions are not the best tool for this, because you will have nested parts of your input text.

In this case it with be more robust to use a parser, but that has a bit of a learning threshold. ANTLR4 is a free parser originally for Java, but it has a version for C# as well. See ANTLR[^]
Note: The code is free but the book isn't, so you need to find examples online
Here is one: Antlr 4 with C# and Visual Studio 2012 | Programming pages[^]

That said, you could try a simpler approach, that might work.
If you first split the input string where you have AND you will get something like this:
Loan no = 12345
(Borrower First Name Contains Milena OR Buy Side Lock Date = 03/13/2016)
Last Finished Milestone = Decisioned OR Subject Property State = NC 
Loan Amt > 50000

This parts will be easier to examine one by one.

If you replace the OR to an AND in the first expression, you will split the parenthesis in two parts.
Loan no = 12345
(Borrower First Name Contains Milena
Buy Side Lock Date = 03/13/2016)
Last Finished Milestone = Decisioned OR Subject Property State = NC
Loan Amt > 50000

This complicates things, but if you have a counter, you will increment when you find a ( and then decrement when you find a ), even if it is in a different part.

Maybe not the most robust of solutions, but it might be worth to give it a try before you get into a real parser.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900