Java使用正则表达式实现高级公式编辑规则校验

代码实现:

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

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

/**
 * @author 2020/11/30
 * 高级公式编辑规则:
 * 正确示例:
 * (1 and 2) or (3 and 4)
 * 1.有and或or,必须要有括号
 * 1 and 2 or 3 and 4
 * 2.一个括号不能同时出现and和or
 * (1 and 2 or 3) and 4
 * 3.不允许出现不存在的编号
 * 1 and 2 and 3 and 4 and 5
 * 4.括号必须成对出现
 * ( 1 and 2 and 3 and 4 and 5
 */
public class ExpressCheckUtil {
    /**
     * 编号正则
     */
    private final static Pattern serialNoRegex = Pattern.compile("[0-9]+");
    /**
     * 子表达式格式1(可单独匹配):(1 and 2 and 3 and 4 .....)
     */
    private final static String subRegex1 = "(\\([0-9]+\\s(and\\s+[0-9]+\\s?)*\\))";
    /**
     * 子表达式格式2(可单独匹配):1 and 2 and 3 and 4 .....
     */
    private final static String subRegex2 = "([0-9]+\\s(and\\s+[0-9]+\\s?)*)";
    /**
     * 子表达式格式3(可单独匹配):(1 or 2 or 3 or 4 .....)
     */
    private final static String subRegex3 = "(\\([0-9]+\\s(or\\s+[0-9]+\\s?)*\\))";
    /**
     * 子表达式格式4(可单独匹配):1 or 2 or 3 or 4 or 4 .....
     */
    private final static String subRegex4 = "([0-9]+\\s(or\\s+[0-9]+\\s?)*)";

    /**
     * 子表达式格式5(不可单独匹配):and 2 and 3 and 4 and (1 or 3) and  .....
     */
    private final static String subRegex5 = "((and\\s+([0-9]+|("+subRegex1+"|"+subRegex3+"))\\s?)*)";
    /**
     * 子表达式格式6(不可单独匹配):or 2 or 3 or 4 or (1 and 2) or .....
     */
    private final static String subRegex6 = "((or\\s+([0-9]+|("+subRegex1+"|"+subRegex3+"))\\s?)*)";

    /**
     * 组合正则表达式1
     * (1 and 2 and 3 and 4 .....)|(1 or 2 or 3 or 4 .....)  and (1 and 2 and 3 and 4 .....)|(1 or 2 or 3 or 4 .....)|1 and 2 and 3 and 4 .....
     * (1 and 2 and 3 and 4 .....)|(1 or 2 or 3 or 4 .....)  or (1 and 2 and 3 and 4 .....)|(1 or 2 or 3 or 4 .....)|1 or 2 or 3 or 4 .....
     */
    private final static String combineRegex1 = "(("+subRegex1+"\\s+("+subRegex5+"|"+subRegex6+"))|("+subRegex3+"\\s+("+subRegex5+"|"+subRegex6+")))";

    /**
     * 组合正则表达式2
     * 1 and 2 and 3 and 4...... and (1 and 2 and 3 and 4 .....)|(1 or 2 or 3 or 4 .....)
     * 1 or 2 or 3 or 4...... or (1 or 2 or 3 or 4 .....)  or (1 and 2 and 3 and 4 .....)|(1 or 2 or 3 or 4 .....)
     */
    private final static String combineRegex2 = "(("+subRegex2+"\\s+("+subRegex5+"))|("+subRegex4+"\\s+("+subRegex6+")))";

    /**
     * 最终匹配所有表达式组合正则
     */
    private static Pattern finalRegex=Pattern.compile(StringUtils.join(new String[]{subRegex1,subRegex2,subRegex3,subRegex4,combineRegex1,combineRegex2},"|"));


    private static void check(String expression, int maxSerialNo) {
        if (StringUtils.isBlank(expression)) {
            throw new IllegalArgumentException("表达式不能为空");
        }
        expression = expression.trim();

        //1.括号必须成对出现
        if (!ParenUtil.isCorrectExpressionParen(expression)) {
            throw new IllegalArgumentException("括号必须成对出现");
        }

        // 2.有and或or,必须要有括号,一个括号不能同时出现and和or
        Matcher expressionMatcher = finalRegex.matcher(expression);
        if (!expressionMatcher.matches()) {
            throw new IllegalArgumentException("有and或or,必须要有括号,一个括号不能同时出现and和or");
        }

        //3.不允许出现不存在的编号
        List<Integer> serialNoList = Lists.newArrayList();
        Matcher serialNoMatcher = serialNoRegex.matcher(expression);
        while (serialNoMatcher.find()) {
            serialNoList.add(Integer.valueOf(serialNoMatcher.group()));
        }
        int max = serialNoList.stream().mapToInt(x -> x).summaryStatistics().getMax();
        if (max > maxSerialNo) {
            throw new IllegalArgumentException("不允许出现不存在的编号->【" + max + "】");
        }

        System.out.println(true);
    }

    public static void main(String[] args) {
        //正确示例: (1 and 2) or (3 and 4)   (1 or 2) and (3 or 4)
        check("1 or 2 or 3 or 4 or (1 and 2) or (1 and 3) or (1 or 4)",4);
        check("1 and 2 and 3 and 4 and (1 or 3) and 2",4);
        check("1 and 2 and 3 and 4 and 2",4);
        check("(1 and 2) or (3 and 4)",4);
        check("(1 or 2) and (3 or 4)",4);
        check("1 or 2 or 3 or 4 ",4);
        check("1 and 2 and 3 and 4 ",4);
        check("(3 or 4) and (3 or 4) and (3 and 4) and (3 or 4)", 4);
        check("(1 or 2 or 4) or (3 and 4) or (1 or 2 or 4)", 4);
        check("(1 or 2 or 4) or (3 and 4) or 1 or 4", 4);
        check("(1 or 2 or 4) and (3 and 4) and 1 and 4", 4);
        check("1 and 2 and 4 and (3 and 4) and 1 and 4 and (3 or 1)", 4);
        check("1 or 2 or 4 or (3 and 4) or 1 or 4 or (1 and 3)", 4);
        check("(1 or 2) or (3 and 4) or (1 or 2 or 4)", 4);
        check("(1 or 2) and (3 or 4)", 4);
        check("(1 or 2) and (3 or 4) and 1 and (1 or 4 or 2)", 4);
        check("(1 or 2) and (3 or 4) and 1 and (1 and 4 and 3)", 4);
        check("(1 and 2 and 3 and 4) and (1 or 2 or 3 or 4) and (1 or 2 or 3 or 4)", 4);
        check("1 and 2 and 3 and 4 and (1 and 2 and 3 and 4)", 4);
        check("1 and 2 and 3 and 4 and (1 or 2 or 3 or 4)", 4);
        check("1 or 2 or 3 or 4 or (1 and 2 and 3 and 4)", 4);
        check("(1 and 2 and 3 and 4) and 2 and 3 and 4", 4);
        check("(1 and 2 and 3 and 4) or 2 or 3 or 4", 4);
        check("(1 or 2 or 3 or 4) and 2 and 3 and 4", 4);
        check("(1 or 2 or 3 or 4) or 2 or 3 or 4", 4);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值