项目开发中实用的正则工具类,只需要配置即可,不用每次都去写一遍判断
package com.luyang.framework.util.verifier;
import java.util.regex.Pattern;
/**
* 常用正则表达式集合。
* @author: Lu hah
* @createTime: 2019-10-11 00:25
*/
public final class PatternPool {
/** 移动电话 */
public final static Pattern MOBILE = Pattern.compile("(?:0|86|\\+86)?1[3456789]\\d{9}");
/** 邮件 符合RFC 5322规范 */
public final static Pattern EMAIL = Pattern.compile("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", Pattern.CASE_INSENSITIVE);
}
package com.luyang.framework.util.verifier;
import com.luyang.framework.util.StringUtil;
import java.util.regex.Pattern;
/**
* 正则相关工具操作类.
* @author: Lu hah
* @createTime: 2019-10-11 00:18
*/
public final class ReUtil {
/**
* 正则表达式验证
* @author Lu hah
* @date 2019/10/11 0:34
* @param pattern 正则模式
* @param value 值
* @return boolean
*/
public static boolean isMactchRegex(Pattern pattern, CharSequence value) {
return isMatch(pattern, value);
}
/**
* 给定类容是否匹配对应正则.
* @author Lu hah
* @date 2019/10/11 0:20
* @param pattern 正则模式
* @param content 内容
* @return boolean
*/
public static boolean isMatch (Pattern pattern, CharSequence content) {
if (null == pattern || StringUtil.isEmpty(content)) {
return false;
}
return pattern.matcher(content).matches();
}
}
package com.luyang.framework.util;
import com.luyang.framework.util.verifier.PatternPool;
import com.luyang.framework.util.verifier.ReUtil;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
/**
* 字符串操作工具.
* @author: Lu Yang
* @createTime: 2019-09-08 05:17
*/
public final class StringUtil {
/** 移动电话 */
private final static Pattern MOBILE = PatternPool.MOBILE;
/** 邮件 */
private final static Pattern EMAIL = PatternPool.EMAIL;
/**
* 手机号码校验
* @author Lu hah
* @date 2019/10/11 0:32
* @param value 值
* @return boolean
*/
public static boolean isMobile (CharSequence value) {
return ReUtil.isMactchRegex(MOBILE, value);
}
/**
* 邮箱校验
* @author Lu hah
* @date 2019/10/11 0:37
* @param value 值
* @return boolean
*/
public static boolean isEmail (CharSequence value) {
return ReUtil.isMactchRegex(EMAIL, value);
}
}
网友评论