國際區號+手機號正則校驗

public enum MobileRegularExp {
    CN("中國","^(\\+?0?86\\-?)?1[345789]\\d{9}$"),
    TW("臺灣","^(\\+?886\\-?|0)?9\\d{8}$"),
    HK("香港","^(\\+?852\\-?)?[569]\\d{3}\\-?\\d{4}$"),
    MS("馬來西亞","^(\\+?6?01){1}(([145]{1}(\\-|\\s)?\\d{7,8})|([236789]{1}(\\s|\\-)?\\d{7}))$"),
    PH("菲律賓","^(\\+?0?63\\-?)?\\d{10}$"),
    TH("泰國","^(\\+?0?66\\-?)?\\d{10}$"),
    SG("新加坡","^(\\+?0?65\\-?)?\\d{10}$"),
    /* 以上是項目可能設計到的市場,一下是其餘國家的手機號校驗正則,歡迎補充*/
    DZ("阿爾及利亞", "^(\\+?213|0)(5|6|7)\\d{8}$"),
    SY("敘利亞","^(!?(\\+?963)|0)?9\\d{8}$"),
    SA("沙特阿拉伯","^(!?(\\+?966)|0)?5\\d{8}$"),
    US("美國","^(\\+?1)?[2-9]\\d{2}[2-9](?!11)\\d{6}$"),
    CZ("捷克共和國","^(\\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$"),
    DE("德國","^(\\+?49[ \\.\\-])?([\\(]{1}[0-9]{1,6}[\\)])?([0-9 \\.\\-\\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$"),
    DK("丹麥","^(\\+?45)?(\\d{8})$"),
    GR("希臘","^(\\+?30)?(69\\d{8})$"),
    AU("澳大利亞","^(\\+?61|0)4\\d{8}$"),
    GB("英國","^(\\+?44|0)7\\d{9}$"),
    CA("加拿大","^(\\+?1)?[2-9]\\d{2}[2-9](?!11)\\d{6}$"),
    IN("印度","^(\\+?91|0)?[789]\\d{9}$"),
    NZ("新西蘭","^(\\+?64|0)2\\d{7,9}$"),
    ZA("南非","^(\\+?27|0)\\d{9}$"),
    ZM("贊比亞","^(\\+?26)?09[567]\\d{7}$"),
    ES("西班牙","^(\\+?34)?(6\\d{1}|7[1234])\\d{7}$"),
    FI("芬蘭","^(\\+?358|0)\\s?(4(0|1|2|4|5)?|50)\\s?(\\d\\s?){4,8}\\d$"),
    FR("法國","^(\\+?33|0)[67]\\d{8}$"),
    IL("以色列","^(\\+972|0)([23489]|5[0248]|77)[1-9]\\d{6}"),
    HU("匈牙利","^(\\+?36)(20|30|70)\\d{7}$"),
    IT("意大利","^(\\+?39)?\\s?3\\d{2} ?\\d{6,7}$"),
    JP("日本","^(\\+?81|0)\\d{1,4}[ \\-]?\\d{1,4}[ \\-]?\\d{4}$"),
    NO("挪威","^(\\+?47)?[49]\\d{7}$"),
    BE("比利時","^(\\+?32|0)4?\\d{8}$"),
    PL("波蘭","^(\\+?48)? ?[5-8]\\d ?\\d{3} ?\\d{2} ?\\d{2}$"),
    BR("巴西","^(\\+?55|0)\\-?[1-9]{2}\\-?[2-9]{1}\\d{3,4}\\-?\\d{4}$"),
    PT("葡萄牙","^(\\+?351)?9[1236]\\d{7}$"),
    RU("俄羅斯","^(\\+?7|8)?9\\d{9}$"),
    RS("塞爾維亞","^(\\+3816|06)[- \\d]{5,9}$"),
    R("土耳其","^(\\+?90|0)?5\\d{9}$"),
    VN("越南","^(\\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$");

    /**
     * 國際名稱
     */
    private String national;

    /**
     * 正則表達式
     */
    private String regularExp;

    public String getNational() {
        return national;
    }

    public void setNational(String national) {
        this.national = national;
    }

    public String getRegularExp() {
        return regularExp;
    }

    public void setRegularExp(String regularExp) {
        this.regularExp = regularExp;
    }

    MobileRegularExp(String national, String regularExp) {
        this.national = national;
        this.regularExp = regularExp;
    }
}

  在發送短信前,經過遍歷這個枚舉,一一校驗java

protected static boolean isMobileNumber(String nationalCode, String mobileNumber) {
        boolean isMobileNumber = false;
        for (MobileRegularExp regularExp : MobileRegularExp.values()) {
            Pattern pattern = Pattern.compile(regularExp.getRegularExp());
            Matcher matcher = pattern.matcher(new StringBuilder().append(nationalCode).append(mobileNumber).toString());
            if (matcher.matches()) {
                isMobileNumber = true;
                // 枚舉中把最經常使用的國際區號拍在前面能夠減小校驗開銷
                break;
            }
        }
        return isMobileNumber;
    }

  轉載:http://www.javashuo.com/article/p-aqjaarqs-hp.html正則表達式

相關文章
相關標籤/搜索