[英]Evaluate boolish expression string at runtime
How can I evaluate expression string that look like the following examples? 如何评估类似于以下示例的表达式字符串? I'm looking for an easy way to parse them.
我正在寻找一种解析它们的简便方法。
string1: "True and False"
bool: false
string1: "False and False"
bool: false
string1: "true or False"
bool: true
As long as it will always follow the simple structure given in your examples, this will work: 只要它始终遵循示例中给出的简单结构,就可以使用:
private static readonly Regex boolParseRegex =
new Regex(@"^(true|false) (and|or) (true|false)$", RegexOptions.IgnoreCase);
public bool ParseExpression(string expr)
{
var res = boolParseRegex.Match(expr);
bool val1 = bool.Parse(res.Groups[1].Value);
bool isAnd = string.Equals(res.Groups[2].Value, "and",
StringComparison.InvariantCultureIgnoreCase);
bool val2 = bool.Parse(res.Groups[3].Value);
return isAnd ? val1 && val2 : val1 || val2;
}
If you want to be able to parse more general expressions (eg "true"
, "(true and false) or true"
, in addition to "false and true"
), you should take a look at Irony , as recommended in convert string expression to a boolean expression 's answer (thanks to Frode for commenting that). 如果您希望能够解析更多的通用表达式(例如,除了
"false and true"
之外还包括"true"
, "(true and false) or true"
"false and true"
),则应该看一下Irony ,如转换字符串中的建议布尔表达式的答案的表达式 (感谢Frode对此进行评论)。
What you want may be achieved if you implement a parser, or use a parsing library. 如果您实现解析器或使用解析库,则可能会实现您想要的。 Basically you need to break down your expression to its components, and then interpret them as logic blocks;
基本上,您需要将表达式分解为各个组成部分,然后将它们解释为逻辑块。 your samples contain two types, values and operators.
您的样本包含两种类型,值和运算符。
Let's try it on your first example, True AND False
: 让我们在第一个示例
True AND False
上尝试一下:
True <- value : Carry it over
AND <- operator : store
False <- Value : Use stored Operation on Carried value. Carry it over.
End of expression
Things get a tad more complicated if you add grouping, because you need to store sequences in a structure often called an expression tree , because it's hierarchical and require you to parse each group, starting from the 'leaves' (furthest away from the root). 如果添加分组,事情会变得更加复杂,因为您需要将序列存储在通常称为表达式树的结构中,因为它是分层的,需要您从“叶”(离根最远)开始解析每个组。 。 Let's use the following example:
让我们使用以下示例:
False XOR (True AND False) OR (True OR False)
The expression tree would look like this: 表达式树如下所示:
Root Group : False XOR G1 OR G2
Group 1 : True AND False
Group 2 : True OR False
Using your expression evaluator, you need to first solve groups 1 and 2: 使用表达评估器,您首先需要求解组1和2:
Root Group : False XOR G1 OR G2
Group 1 : False
Group 2 : True
Then apply it to the Root group: 然后将其应用于“根”组:
Root Group : False XOR False OR True
False <- value : Carry it over
XOR <- operator : store
False <- value : Apply stored Operation (result True). Carry it over.
AND <- operator : store
False <- Value : Apply stored Operation (result True). Carry it over.
- End of expression
That's basically how expression parsers works. 基本上,这就是表达式解析器的工作方式。
Edit: Irony , pointed out by Frode on a comment on your question, is a well-known language parser that might do the trick for you. 编辑:弗洛德(Frode)在对您的问题的评论中指出的讽刺意味深长,它是一种知名的语言解析器,可以为您解决问题。 I remember reading about it a while ago, but he was way faster than me.
我记得有一阵子读过,但是他比我快。 =)
=)
something simple could be: 一些简单的事情可能是:
static bool string_parser(string str_to_parse)
{
bool result= false;
string[] parts = str_to_parse.Split(' ');
if (parts.Length == 3)
{
if (parts[0].ToLower().Contains("true"))
{
result = true;
}
if (parts[1].ToLower().Contains("or"))
{
if (parts[2].ToLower().Contains("true"))
{
result= result || true;
}
}
else if (parts[1].ToLower().Contains("and"))
{
result = parts[2].ToLower().Contains("true") ? result && true : result && false;
}
}
return result;
}
but I wonder False && False should not be 'True' in this example false && false is false 但是我想知道False && False在这个例子中不应该是'True'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.