简体   繁体   English

如何在C#中评估自定义括号表达式?

[英]How to evaluate custom parenthesis expression in C#?

I am working on an Advanced Search feature where the expression I need to evaluate will look something like this with the parenthesis in place: 我正在开发一个高级搜索功能,其中我需要评估的表达式看起来像是这样的括号:

((Loan number is 1000 And
Lock Date is less than 12/03/2015) Or
Borrower SSN contains 12345) And
((Buy date is between 12/01/2015 and 23/02/2016 And
APR is less than 20000) Or
Loan amount is greater than 60000)

Or in simple words 或者用简单的话说

((condition1 And condition2) Or condition 3) And ((condition4 And condition5) Or condition6).

If we look at the parenthesis, the condition1 and condition2 has to be evaluated first and then the output of this is executed with condition 3 and so on.... 如果我们查看括号,必须首先计算condition1和condition2,然后在条件3的情况下执行this的输出,依此类推......

We have API to evaluate two conditions at a time. 我们有API来一次评估两个条件。 However the challenge in this context is 然而,在这种背景下的挑战是

1) How to identify the corresponding parenthesis and evaluate them first. 1)如何识别相应的括号并首先对其进行评估。 And then use this intermediate result for further evaluation?. 然后使用这个中间结果进行进一步评估?

2)How to find unused parenthesis? 2)如何找到未使用的括号? For example (((condition1 And condition2))), in this case the though it is not required there are 3 starting and 3 closing parenthesis which is a valid expression. 例如(((condition1 And condition2))),在这种情况下虽然不需要,但有3个起始括号和3个右括号,它是一个有效的表达式。

I tried finding some algorithm here and here 我试着在这里这里找到一些算法

However this takes token based manipulation which reads one character at a time and it is an arithmetic expression evaluation that computer understands. 然而,这需要基于令牌的操作,该操作一次读取一个字符,并且它是计算机理解的算术表达式评估。 In my case these things are custom and we should find a algorithm to do this. 在我的情况下,这些东西是自定义的,我们应该找到一个算法来做到这一点。 Can anyone suggest a better approach for my scenario? 任何人都可以为我的场景建议更好的方法吗?

If I understand you correctly, you already got the expression evaluator. 如果我理解正确,你已经有了表达式评估器。 What you need is to split the evaluations according to the parenthesis. 您需要的是根据括号分割评估。 I'd use a loop, in which I'd find inner parenthesis groups, using this regex: 我使用一个循环,我在其中找到内括号组,使用此正则表达式:

\(([^()]*)\)

Then, if found, replace them with the result of your evaluation routine, and repeat until a final string remains, without parenthesis. 然后,如果找到,则将其替换为评估例程的结果,并重复直到最后一个字符串保留,不带括号。

Pseudo code: 伪代码:

Find a string enclosed by (), not containing any ()
If found
    Replace it with the evaluated value of the string (including parenthesis)
    Go again
Return result

About the unused parenthesis, let them be treated the same. 关于未使用的括号,让它们处理相同。 They'll end up in your evaluation routine as a single value. 它们最终会作为单个值进入您的评估例程。

Check this fiddle . 检查这个小提琴 Instead of evaluating it returns a random number, 0 or 1 , but it demonstrates the logic. 而不是评估它返回一个随机数, 01 ,但它演示了逻辑。

Hope this helps. 希望这可以帮助。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM