package com.sunyard.ssp.utils.regexp;

import com.sunyard.ssp.utils.regexp.model.OrdinaryRegexNode;
import com.sunyard.ssp.utils.regexp.model.RegexNode;

import java.util.regex.Pattern;

/**
 * @author:tsz
 * @date:2020/4/14
 * @description:
 */
public class RegexUtil {

    /**
    *@author ：tsz
    *@date   ：2020/04/14 17:56
    *@description : 根据表达式生成随机数
    */
    public static String genByExpression(String expression){
        RegexNode node = null;
        try {
            node = new OrdinaryRegexNode(expression);
            Pattern pattern = Pattern.compile(expression);
            String random = node.random();
            if (pattern.matcher(random).matches()) {
                return random;
            } else {
                genByExpression(expression);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    /**
    *@author ：tsz
    *@param  ：expression 表达式
    *@param  ：number 要校验的值
    *@date   ：2020/04/14 17:55
    *@description : 参数校验 true校验成功false失败
    */
    public static Boolean checkNameByExpression(String expression,String number){
        Pattern pattern = Pattern.compile(expression);
        if (pattern.matcher(number).matches()) {
            return true;
        }
        return false;

    }
}
