问题描述
是否有可能将一个字符串的前pression转换成布尔条件?
Is it possible to convert a string expression into a boolean condition?
例如,我得到以下字符串:
For example, I get the following string:
var b = "32 < 45 && 32 > 20"
我想出来的这个创建一个布尔
前pression并调用它。再presentation的字符串也灵活(使它更有趣),所以它可以让 ||,&安培;&安培;,()
I would like to create a bool
expression out of this and invoke it. The string representation is also flexible (to make it more fun), so it allows ||, &&, ()
.
推荐答案
我会用的.NET语言包。你可以构造一个简单的语法与反讽,然后解析字符串转换为可执行的命令。这里有一个arthmetic语法在这并在一个体面的例子<一href=\"http://irony.$c$cplex.com/wikipage?title=Ex$p$pssion%20Grammar%20sample&referringTitle=Home\"相对=nofollow>防爆pression语法样品,它的一个pretty常见的请求;)
I would use Irony, the .NET language kit. You could construct a simple grammar with Irony and then parse the string into executable command. There's a decent example of an arthmetic grammar in this tutorial and in the Expression Grammar Sample, its a pretty common request ;)
我绝对建议使用合适的编译器,而不是正则表达式或滚你自己的方法 - 这将是更加可扩展的,如果你想添加更多的规则
I definitely suggest using a proper compiler as opposed to Regex or a roll your own approach - it will be much more extensible if you ever want to add more rules.
这篇关于C#转换字符串前pression为布尔前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!