package com.changhong.epc.count.service.system.performer.verify; import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; import static com.changhong.epc.bean.count.func.FunNameKey.FUNC_GET_VAL_PARAM_MASTER; /** * 函数格式验证 * * @author wangZX * */ public class FuncFormatVerify { /** * 处理特殊函数 02 * @param args */ public static String pretreatmentFun(String str) { // // System.out.println(FuncFormatVerify.partenMatch("(1+(2)*(1+2))=9")); [{|']?[^}|']+[}|']? String pattern = "[{][0-9]+[}][\\s]*[=][\\s]*(([{][0-9]+[}])|(['][^']+['])|([0-9]+))"; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(str); while(m.find()){ String group = m.group(); String[] split = group.split("="); String funcName = FUNC_GET_VAL_PARAM_MASTER+"(" + split[0] + ", " + split[1] + ")"; str = str.replace(group, funcName); } return str; } /** * 验证 括号是否匹配 01 * @param exp * @return */ public static boolean partenMatchFun(String exp) { Stack stack = new Stack(); for (int i = 0; i < exp.length(); i++) { switch (exp.charAt(i)) { case '{': case '(': case '[': stack.push(exp.charAt(i)); break; case '}': case ')': case ']': if (stack.isEmpty()) return false; if (!FuncFormatVerify.match(stack.pop(), exp.charAt(i))) return false; break; } } if (!stack.isEmpty()) return false; return true; } private static boolean match(char strright, char strleft) { switch (strright) { case '{': if (strleft == '}') return true; break; case '(': if (strleft == ')') return true; break; case '[': if (strleft == ']') return true; break; default: break; } return false; } }