java 正則工具類

項(xiàng)目開發(fā)中實(shí)用的正則工具類,只需要配置即可,不用每次都去寫一遍判斷

package com.luyang.framework.util.verifier;

import java.util.regex.Pattern;

/**
 * 常用正則表達(dá)式集合。
 * @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 {

    /**
     * 正則表達(dá)式驗(yàn)證
     * @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;

   

    /**
     * 手機(jī)號碼校驗(yàn)
     * @author Lu hah
     * @date 2019/10/11 0:32
     * @param value 值
     * @return boolean
     */
    public static boolean isMobile (CharSequence value) {
        return ReUtil.isMactchRegex(MOBILE, value);
    }

    /**
     * 郵箱校驗(yàn)
     * @author Lu hah
     * @date 2019/10/11 0:37
     * @param value 值
     * @return boolean
     */
    public static boolean isEmail (CharSequence value) {
        return ReUtil.isMactchRegex(EMAIL, value);
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容