How to evaluate custom parenthesis expression in C#?

后端 未结 1 951
暗喜
暗喜 2020-12-19 22:18

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         


        
相关标签:
1条回答
  • 2020-12-19 23:24

    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.

    Hope this helps.

    Regards.

    0 讨论(0)
提交回复
热议问题