java中正則匹配

/**
* java中正則匹配的對象:
* pattern:
*    Pattern   Pattern.complie(regexString)
*    Macther   Pattern.matches(regexString)
* Matcher:
*    boolean   matcher.find() //查找下一個匹配對象
*    String    matcher.guorp() //返回整個匹配模式匹配到的結果
*    boolean   matcher.matches() //嘗試將整個區域與模式匹配
*    int      matcher.groupCount() //返回匹配規則的分組,如:(aa)(bb):這表示兩組
*    String matcher.group(int group) //返回匹配對象對應分組的匹配結果
*    MatcheResult matcher.toMatchResult() //將匹配結果一MatchResult的形式返回
*/

public final class RegExpValidatorUtils 
{
    /**
     * 驗證郵箱
     * 
     * @param 待驗證的字符串
     * @return 如果是符合的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean isEmail(String str) 
    {
        String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        return str.matches(regex);
    }

    /**
     * 驗證IP地址
     *  
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean isIP(String str) 
    {
        String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
        String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
        return str.matches(regex);
    }

    /**
     * 驗證網址Url
     *
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsUrl(String str) 
    {
        String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
        return str.matches(regex);
    }

    /**
     * 驗證電話號碼
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsTelephone(String str) 
    {
        String regex = "^(\\d{3,4}-)?\\d{6,8}$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入密碼條件(字符與數據同時出現)
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsPassword(String str) 
    {
        String regex = "[A-Za-z]+[0-9]";
        return str.matches(regex);
    }

    /**
     * 驗證輸入密碼長度 (6-18位)
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsPasswLength(String str) 
    {
        String regex = "^\\d{6,18}$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入密碼長度 (6-18位)格式是數字和字母!
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsPasswLength(String str) 
    {
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入郵政編號
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsPostalcode(String str) 
    {
        String regex = "^\\d{6}$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入手機號碼
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsHandset(String str) 
    {
        String regex = "^[1]+[3,5]+\\d{9}$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入身份證號
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsIDcard(String str) 
    {
        String regex = "(^\\d{18}$)|(^\\d{15}$)";
        return str.matches(regex);
    }

    /**
     * 驗證輸入兩位小數
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsDecimal(String str) 
    {
        String regex = "^[0-9]+(.[0-9]{2})?$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入一年的12個月
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsMonth(String str) 
    {
        String regex = "^(0?[[1-9]|1[0-2])$";
        return str.matches(regex);
    }

    /**
     * 驗證輸入一個月的31天
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsDay(String str) 
    {
        String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
        return str.matches(regex);
    }

    /**
     * 驗證日期時間
     * 
     * @param 待驗證的字符串
     * @return 如果是符合網址格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean isDate(String str) 
    {
        // 嚴格驗證時間格式的(匹配[2002-01-31], [1997-04-30],
        // [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])
        // String regex ="^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";

        // 沒加時間驗證的YYYY-MM-DD
        // String regex ="^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";

        // 加了時間驗證的YYYY-MM-DD 00:00:00
        String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";
        return str.matches(regex);
    }

    /**
     * 驗證數字輸入
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsNumber(String str) 
    {
        String regex = "^[0-9]*$";
        return str.matches(regex);
    }

    /**
     * 驗證非零的正整數
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsIntNumber(String str)
    {
        String regex = "^\\+?[1-9][0-9]*$";
        return str.matches(regex);
    }

    /**
     * 驗證大寫字母
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsUpChar(String str) 
    {
        String regex = "^[A-Z]+$";
        return str.matches(regex);
    }

    /**
     * 驗證小寫字母
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsLowChar(String str) 
    {
        String regex = "^[a-z]+$";
        return str.matches(regex);
    }

    /**
     * 驗證驗證輸入字母
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsLetter(String str) 
    {
        String regex = "^[A-Za-z]+$";
        return str.matches(regex);
    }

    /**
     * 驗證驗證輸入漢字
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsChinese(String str) 
    {
        String regex = "^[\u4e00-\u9fa5],{0,}$";
        return str.matches(regex);
    }

    /** 
     * 驗證驗證輸入字符串
     * 
     * @param 待驗證的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否則爲 <b>false </b>
     */
    public static boolean IsLength(String str) 
    {
        String regex = "^.{8,}$";
        return str.matches(regex);
    }



}

以上只是轉載總結的現成正則表達式。
具體如何學請參考http://www.runoob.com/regexp/regexp-tutorial.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章