Java正則驗證


手寫正則匹配工具類,以便於以后使用方便,后續有空了不斷優化補充....................

常量類

public class RegexConstant {
    //匹配Email
    public static final int E_MAIL=0;
    //匹配手機
    public static final int MOBIL_PHONE=1;
    //匹配電話座機
    public static final int TEL_PHONE=2;
    //匹配郵編
    public static final int ZIP_CODE=3;
    //匹配URL
    public static final int URL=4;
    //匹配ip格式
    public static final int IP=5;
    //匹配HTML標簽
    public static final int HTML_TAG=6;
    //匹配十六進制
    public static final int HEX=7;
    //匹配QQ號
    public static final int QQ=8;
    //匹配身份證號
    public static final int ID_CARD=9;
    //匹配正整數
    public static final int POSITIVE_INTEGER=10;
}

 工具類

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
    //正則表達式的字符串
    private String regexStr=new String();

    //正則校驗,如果創建對象但是不傳入正則表達式而調用該方法就會報錯
    public  boolean customMatcher(String testStr){//傳入待檢測的字符串
        Pattern pattern=Pattern.compile(regexStr);
        Matcher matcher = pattern.matcher(testStr);
        return matcher.matches();
    }
    //正則表達式通用匹配
    private static boolean genericMatcher(String regexExpre,String testStr){
        Pattern pattern=Pattern.compile(regexExpre);
        Matcher matcher = pattern.matcher(testStr);
        return matcher.matches();
    }

    //匹配器
    public static boolean matcher(int RegexConstant_NAME,String testStr){
        boolean flag=false;
        switch (RegexConstant_NAME){
            case RegexConstant.E_MAIL:flag=genericMatcher("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*",testStr);break;
            case RegexConstant.HEX:flag=genericMatcher("/^#?([a-f0-9]{6}|[a-f0-9]{3})$/",testStr);break;
            case RegexConstant.IP:flag=genericMatcher("/^(?:(?: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]?)$/",testStr);break;
            case RegexConstant.HTML_TAG:flag=genericMatcher("/^<([a-z]+)([^<]+)*(?:>(.*)<\\/\\1>|\\s+\\/>)$/",testStr);break;
            case RegexConstant.ZIP_CODE:flag=genericMatcher("/^[u4e00-u9fa5],{0,}$/",testStr);break;
            case RegexConstant.QQ:flag=genericMatcher("[1-9][0-9]{4,}",testStr);break;
        }
        return flag;
    }
    public RegexUtil(String regexStr) {
        this.regexStr = regexStr;
    }

    public String getRegexStr() {
        return regexStr;
    }

    public void setRegexStr(String regexStr) {
        this.regexStr = regexStr;
    }
}

 

 目前API說明

需求1:使用自定義的正則表達式並校驗我的待匹配字符串是否符合要求

customMatcher(String testStr) 自定義正則匹配的方法

如果使用自定義正則匹配表達式進行字符串匹配,需要通過有參構造先傳入正則表達式創建RegexUtil的對象,通過對象調用customMatcher(String testStr)傳入待匹配的字符串進行匹配,為預防空指針異常,本類沒有提供無參構造

System.out.println(new RegexUtil("[0-9]{3}").customMatcher("213"));

 結果為true

需求2:使用工具類提供的表達式進行匹配字符串

genericMatcher(String regexExpre,String testStr)通用正則匹配器

只需要通過RegexUtil.genericMatcher(String regexExpre,String testStr)即可實現字符串匹配,第一個參數是預先定義的常量,第二個表達式是待匹配的字符串,如

boolean reslut = RegexUtil.matcher(RegexConstant.E_MAIL, "1433123@163.com");
System.out.println(reslut);

 結果為true

 后續需求待完善中....


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM