public static void main(String[] args){
String keyExp="你好 and 我好 or 他好 and not 不好";
//表达式转换
String[] exp=keyExp.replace(" and ","&&").replace(" or ","||").replaceAll("^not ","!").replace(" not ","!");
//替换所有的"(",")",并按照and,or,not进行分割
String[] keyExpSplits=keyExp.replace("(","").replace(")","").split(" and | or | not |^not ");
String verifyExp=exp;//用于验证的表达式
for(String key:keyExpSplits){
if(key.trim().equals(""))
continue;
verifyExp=verifyExp.replace(key,"false");
}
//对表达式进行验证
FelEngine fel=new FelEngineImpl();
FelContext ctx=fel.getContext();
try{
fel.eval(verifyExp,ctx);
}catch(Exception e){
System.out.println("表达式出错");
e.printStackTrance();
}
System.out.println("表达式正确");
}
Fel表达式引擎使用样例-验证逻辑表达式是否规范
最新推荐文章于 2021-12-08 21:26:40 发布