如何在C#中验证括号位置? [英] How to validate parentheses position in C#?

查看:134
本文介绍了如何在C#中验证括号位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#在字符串表达式中进行括号位置验证。表达式如下所示。



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





这里AND和OR是可以在两个条件之间执行的两种不同操作。我们在C#中使用API​​来评估包含操作的两个条件。



有了这些东西,最终用户可以在两个或多个条件之间放置来自UI的括号,以便优先评估这些条件。然而,UI允许他们仅在条件的开头或结尾放置括号。



让我们在上面的例子中说,如果用户想要评估第二和第三优先条件他/她将括号括在它们之间如下所示。





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.

推荐答案

正则表达式不是最佳工具,因为您将拥有输入文本的嵌套部分。



在这种情况下,使用解析器会更加健壮,但这有一点学习门槛。 ANTLR4是一个最初用于Java的免费解析器,但它也有一个C#版本。请参见 ANTLR [ ^ ]

注意:代码是免费的,但书不是,所以你需要在网上找到例子

这是一个:使用C#和Visual Studio的Antlr 4 2012 |编程页面 [ ^ ]



也就是说,你可以尝试一种更简单的方法,这可能有用。

如果您首先将输入字符串拆分为您所在的位置,您将得到以下内容:

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



这部分将更容易逐一检查。



如果你将OR替换为在第一个表达式中,您将括号分为两部分。


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.


这篇关于如何在C#中验证括号位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆