开发者

Validate a string to contain only logical operators and some conditions

I am trying to validate a string which can contain some conditions and logical operators. For example, My input expression could be something like this:

(var1==var2 AND var2==var4) OR (someothervar!=5) AND (somemorevar=<4 OR var5<10)
开发者_开发知识库

Later, I would be converting this string to a java readable expression,

(var1==var2 && var2==var4) || (someothervar!=5) && (somemorevar=<4 || var5<10)

The input string is captured at runtime, and it can be anything. My concern is to validate the input string to be a valid expression, so later only AND and OR could be replaced with && and || to evaluate.


It looks like you want to build an syntactic tree. Take a look at JavaCC ant ANTLR


Why are you allowing "AND" and "OR", if you need to already validate the input and you are still going to replace the words "AND" and "OR" anyways? Just simply require || and && as part of the validation of the input and skip the step of allowing "AND" and "OR".

A thought on validation would be to perform a regular expression on the input string. I am not very savvy at Regex, but my guess is there would be someone out there that could get you a Regex that would make sure the open parenthesis match the ending and that there are at least two "=" signs, etc. Just some thoughts.


It sounds like you are building a rules engine/parser and passing in rules in a more human-readable format?

If that is the case, you should look at existing rules engines like Drools. The rules syntax is in an easily readable format, and the rules engine is very powerful you way do so much more with it if your rules get more complex.


Try out JEP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜