C#-解析包含逻辑运算符的复杂字符串 [英] C# - parsing a complex string containing logical operators

查看:409
本文介绍了C#-解析包含逻辑运算符的复杂字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从其他人编写的包含以下内容的表达式的配置文件中读取了逻辑字符串:

(VALUE_1)OR((NOT(VALUE_2))AND(NOT(VALUE_3)))

但是,对于在哪里开始对此进行解析并比较以其他字符串的相同字符串名称存储的变量的值,我有些困惑.我认为LambdaExpression是需要使用的东西是正确的吗?字符串是否需要以某种方式拆分并作为组成部分而不是整体进行分析?

Flee 似乎可以满足我的要求,我可以将VALUE_x的名称定义为在使用该库评估表达式之前为true或false.

解决方案

在C#中评估字符串表达式的典型方法是构建表达式树并将其编译为委托(.NET框架涵盖了此工作).在大多数情况下动态linq库<建议使用/a>,但它有一些缺点:不支持将其用作可重用的库(实际上,它只是Scott Gu发布的LINQ功能的例证),并且它只能评估强类型表达式,这在大多数现实生活应用程序中都是很糟糕的. /p>

我建议使用更好的替代方法:来自 NReco Commons 的lambda表达式解析器(这是免费的开放源代码图书馆).它还构建了表达式树,但是使用了完全不同的方法来对表达式进行解析并将其评估为表达式树:它在运行时执行所有类型的协调和调用(如动态语言),支持属性和方法调用,数组构造和条件运算符.一些例子:

var lambdaParser = new NReco.LambdaParser();

var varContext = new Dictionary<string,object>();
varContext["pi"] = 3.14M;
varContext["one"] = 1M;
varContext["two"] = 2M;
varContext["test"] = "test";
varContext["arr1"] = new double[] { 1.5, 2.5 };
Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5
Console.WriteLine( lambdaParser.Eval(" arr1[0]+arr1[1] ", varContext) ); // -> 4
Console.WriteLine( lambdaParser.Eval(" (new[]{1,2})[1]  ", varContext) ); // -> 2

(更多示例和文档可在NReco Commons库页面找到)

I've got a logical string read in from a configuration file written by someone else that contains expressions such as the following:

(VALUE_1)OR((NOT(VALUE_2))AND(NOT(VALUE_3)))

However, I'm a little stumped as to where to start parsing this and comparing the values of the variables that I have stored as the same string name elsewhere. Am I correct in thinking LambdaExpression is the thing that needs to be used? Does the string need splitting in some way and to be analysed as the constituent parts rather than as a whole?

EDIT:

It seems as though Flee does what I need it to do, I can define the names of the VALUE_x as true or false before evaluating the expression using that library.

解决方案

Typical approach for evaluating string expressions in C# is building expression tree and compiling it into delegate (this job is covered by .NET framework). In most cases dynamic linq library is recommended but it has several drawbacks: it is not supported as reusable library (actually it is just illustration of LINQ capabilities published by Scott Gu) and it can evaluate only strongly typed expressions which is bad in most real life applications.

I suggest better alternative: lambda expressions parser from NReco Commons (this is free and open source library). It also builds expression tree but uses quite different approach to expression parsing and evaluating it as expression tree: it performs all types harmonization and invocations at runtime (like dynamic languages), supports property and methods calls, arrays construction and conditional operator. Some examples:

var lambdaParser = new NReco.LambdaParser();

var varContext = new Dictionary<string,object>();
varContext["pi"] = 3.14M;
varContext["one"] = 1M;
varContext["two"] = 2M;
varContext["test"] = "test";
varContext["arr1"] = new double[] { 1.5, 2.5 };
Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5
Console.WriteLine( lambdaParser.Eval(" arr1[0]+arr1[1] ", varContext) ); // -> 4
Console.WriteLine( lambdaParser.Eval(" (new[]{1,2})[1]  ", varContext) ); // -> 2

(more examples and documentation could be found at NReco Commons library page)

这篇关于C#-解析包含逻辑运算符的复杂字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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