1、正则表达式验证-工具类
@Slf4j
public class RegValidateUtils {
//匹配标准通配符
private static final String REGEXP = "\\$\\{\\w{0,}[-+]{0,}\\d{0,}\\}";
/**
* 解析字符串中yyyymmdd格式内容
*/
private static final String PARSE_DATE = "[1-9]\\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])";
/**
* 匹配yyyymmdd格式
*/
private static final String MATCHER_DATE = "^[2]\\d{3}[0|1][0-9][0-3]\\d$";
//标准日期格式yyyymmdd|yyyymm
private static final String STANDARDDATE = "^yyyymmdd$|^yyyymm$";
//匹配日期格式串yyyymm-+n/yyyymmdd-+n
private static final String DATAPARTERN = "^yyyymmdd[-+]\\d{1}$|^yyyymm[-+]\\d{1}$";
//匹配周时间格式,如:201911(2019年第11周)
private static final String WEEK = "^week$";
//匹配版本和adcode
private static final String DATAVERSION = "^data_version$";
private static final String ADCODE = "^adcode$";
/**
* 获取字符串中数字
*/
private static final String NUMBER ="[^0-9]";
private static final String ISNUMBER = "^-?\\d+(\\.\\d+)?$";
/**
* 获取字符串中数字英文字母
*/
private static final String NUMBERLETTER ="[^a-zA-Z0-9]";
//手机号码
public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
//邮箱
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
//url
public static final String REGEX_URL = "http(s)?://.*";
//日期
public static final String REGEX_SIMPLE_DATE1 = "\\d{4}-\\d{2}-\\d{2}";
/**
* 字符串匹配
* @param regex
* @param str
* @return
*/
public static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.find();
}
/**
* 判断通配符中是否存在非法字符
* @param content
* @return
*/
public static boolean validateLegalStr(String content) {
Pattern r = Pattern.compile(REGEXP);
Matcher m = r.matcher(content);
while(m.find()) {
if (!isDateFormat(m.group().replace("${","}").replace("}",""))){
SQLParserUtils.catchError("通配符不合法");
return false;
}
}
return true;
}
/**
* 校验通配符中字符格式是否为准确的日期格式
* @param content
*/
public static boolean isDateFormat(String content){
if (match(STANDARDDATE,content)){
return true;
}
if (match(DATAPARTERN,content)){
return true;
}
if (match(DATAVERSION,content)){
return true;
}
if (match(ADCODE,content)){
return true;
}
if (match(WEEK,content)) {
return true;
}
return false;
}
/**
* 解析指标ID
* @param exp
* @return
*/
public static List<String> getExpParse(String exp) {
if (Objects.isNull(exp)){
return null;
}
Pattern r = Pattern.compile(REGEXP);
Matcher m = r.matcher(exp);
List<String> indexs = new ArrayList<>();
while (m.find()) {
indexs.add(m.group().replace("${","}").replace("}",""));
}
return indexs;
}
/**
* 解析字符串中yyyymmdd格式内容
* @param date
* @return
*/
public static String getDateParse(String date) {
if (Objects.isNull(date)){
return "";
}
Pattern r = Pattern.compile(PARSE_DATE);
Matcher m = r.matcher(date);
String indexs = "";
while (m.find()) {
indexs = m.group();
}
return indexs;
}
/**
* 获取解析出来当前天的日期
* @param date
* @return
*/
public static String getCurDateParse(String date) {
if (Objects.isNull(date)){
return "";
}
Pattern r = Pattern.compile(PARSE_DATE);
Matcher m = r.matcher(date);
String curDate = DateUtils.getDate("yyyyMMdd");
while (m.find()) {
String indexs = m.group();
//判断通过正则表达式解析出的日期和当前日期相等
if(curDate.equals(indexs)){
return indexs;
}
}
return "";
}
/**
* yyyyMMdd格式校验
* @param date
* @return
*/
public static boolean checkDateStyle(String date){
boolean flag = false;
Pattern r = Pattern.compile(MATCHER_DATE);
Matcher m = r.matcher(date);
if(m.matches()){
flag = true;
}
return flag;
}
/**
* 获取字符串中数字
* @param str
* @return
*/
public static int getStrNumber(String str){
Pattern r = Pattern.compile(NUMBER);
Matcher m = r.matcher(str);
if(isNumber(m.replaceAll("").trim())){
return Integer.valueOf(m.replaceAll("").trim());
}
return 0;
}
/**
* 判断是否数字
* @param str
* @return
*/
public static boolean isNumber(String str){
if (str == null) {
return false;
}
Pattern pattern = Pattern.compile(ISNUMBER);
return pattern.matcher(str).matches();
}
/**
* 获取字符串中数字英文字母
* @param str
* @return
*/
public static String getStrNumberLetter(String str){
Pattern r = Pattern.compile(NUMBERLETTER);
Matcher m = r.matcher(str);
String numberLetter = m.replaceAll("").trim();
return numberLetter;
}
/**
* 提取全局参数中时间参数
* @param params
* @return
*/
public static String getDateParam(String params) {
if (StringUtils.isEmpty(params)) {
return null;
}
String reg = "\\r|\\n|\\\\s";
String str = params.replaceAll(reg, "");
String[] split = str.toLowerCase().split("and");
for (String param : split) {
if (param.contains("yyyymm")) {
return param;
}
if (param.contains("yyyymmdd")) {
return param;
}
if (param.contains("week")) {
return param;
}
}
return params;
}
/**
* @Param: [email]
* @Desc: 校验是否是邮箱格式
**/
public static boolean isEmail(String email) {
return Pattern.matches(REGEX_EMAIL, email);
}
/**
* @Param: 校验是否是url格式
* @Desc:
**/
public static boolean isUrl(String url) {
if(StringUtils.isEmpty(url)){
return false;
}
return Pattern.matches(REGEX_URL, url);
}
/**
* @Param: 校验是否是手机号
* @Desc:
**/
public static boolean isMobile(String mobileNum){
return Pattern.matches(REGEX_MOBILE,mobileNum);
}
public static boolean isDateStr1(String dateStr){
return Pattern.matches(REGEX_SIMPLE_DATE1,dateStr);
}
}