項目開發(fā)中實用的正則工具類,只需要配置即可除秀,不用每次都去寫一遍判斷
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規(guī)范 */
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;
/**
* 正則相關(guān)工具操作類.
* @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);
}
/**
* 給定類容是否匹配對應(yīng)正則.
* @author Lu hah
* @date 2019/10/11 0:20
* @param pattern 正則模式
* @param content 內(nèi)容
* @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);
}
}