Enjoy 中文表达式处理

  • • 发表于2019-01-20 10:18:35.0
  • • 作者 小徐同学
  • • 531 次浏览
  • • 0 条评论
  • • 最后编辑时间 2019-01-21 13:19:31.0
  • • 来自 [笔 记]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处


import com.jfinal.kit.ElKit;
import com.jfinal.kit.Kv;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tanyaowu
 *
 */
public class Const {
    /**
     * 用于群聊的group id
     */
    public static final String GROUP_ID = "showcase-websocket";

    public static final String HREAT_MSG = "心跳内容";

    public static void main(String[] args) {

        //        String str = "#if(10000 <=1895)\n" +
//                "\t2529*0.08+3188*0.02+2529*0.01\n" +
//                "#else if(1895<10000 && 10000<=1895*3)\n" +
//                "\t10000*0.11\n" +
//                "#else\n" +
//                "\t12303*0.11\n" +
//                "#end";


        String str = "如果([应发合计'1'] <=1895)\n" +
                "\t2529*0.08+3188*0.02+2529*0.01\n" +
                "或者如果(1895<[应发合计'1'] 且 [应发合计'1']<=1895*3)\n" +
                "\t[应发合计'1']*0.11\n" +
                "否则\n" +
                "\t12303*0.11\n" +
                "结束";

        String p1 = "\\[([^\\[\\]]+)\\]";
        Pattern r = Pattern.compile(p1);
        Matcher m = r.matcher(str);
        while (m.find()){
            System.out.println(m.group());
            str = str.replace(m.group(),"10000");
        }


        str = str.replaceAll("或者如果","#else if");
        str = str.replaceAll("如果","#if");
        str = str.replaceAll("否则","#else");
        str = str.replaceAll("结束","#end");
        str = str.replaceAll("且","&&");
        str = str.replaceAll("或","||");
        System.out.println(str);



        //利用enjoy处理if逻辑
        Template templateByString = Engine.use().getTemplateByString(str);
        String s = templateByString.renderToString(Kv.by("k", "v"));
        System.out.println(s);

        //字符串表达式计算
        Object eval = ElKit.eval(s);
        System.out.println(eval);
        System.out.println(Double.parseDouble(String.valueOf(eval)));

    }
}
评论区(共0条评论)
0条评论
Ctrl+Enter
作者

Michael

小徐同学

帖子:36 回复:0

全栈搬运工

作者详情》
Top