java 正則匹配三大運營商號段

package com.linbilin.phone;  
  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
  
public class CheckPhone {  
  
    /** 電話格式驗證 **/  
    private static final String PHONE_CALL_PATTERN = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}(-\\d{1,4})?$";  
  
    /** 
     * 中國電信號碼格式驗證 手機段: 133,153,180,181,189,177,1700 
     * **/  
    private static final String CHINA_TELECOM_PATTERN = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";  
  
    /** 
     * 中國聯通號碼格式驗證 手機段:130,131,132,155,156,185,186,145,176,1709 
     * **/  
    private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";  
  
    /** 
     * 中國移動號碼格式驗證 
     * 手機段:134,135,136,137,138,139,150,151,152,157,158,159,182,183,184 
     * ,187,188,147,178,1705 
     * **/  
    private static final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";  
    /** 
     * 驗證電話號碼的格式 
     *  
     * @author LinBilin 
     * @param str 
     *            校驗電話字符串 
     * @return 返回true,否則爲false 
     */  
    public static boolean isPhoneCallNum(String str) {  
  
        return str == null || str.trim().equals("") ? false : match(  
                PHONE_CALL_PATTERN, str);  
    }  
  
    /** 
     * 驗證【電信】手機號碼的格式 
     *  
     * @author LinBilin 
     * @param str 
     *            校驗手機字符串 
     * @return 返回true,否則爲false 
     */  
    public static boolean isChinaTelecomPhoneNum(String str) {  
  
        return str == null || str.trim().equals("") ? false : match(  
                CHINA_TELECOM_PATTERN, str);  
    }  
  
    /** 
     * 驗證【聯通】手機號碼的格式 
     *  
     * @author LinBilin 
     * @param str 
     *            校驗手機字符串 
     * @return 返回true,否則爲false 
     */  
    public static boolean isChinaUnicomPhoneNum(String str) {  
  
        return str == null || str.trim().equals("") ? false : match(  
                CHINA_UNICOM_PATTERN, str);  
    }  
  
    /** 
     * 驗證【移動】手機號碼的格式 
     *  
     * @author LinBilin 
     * @param str 
     *            校驗手機字符串 
     * @return 返回true,否則爲false 
     */  
    public static boolean isChinaMobilePhoneNum(String str) {  
  
        return str == null || str.trim().equals("") ? false : match(  
                CHINA_MOBILE_PATTERN, str);  
    }  
      
  
    /** 
     * 驗證手機和電話號碼的格式 
     *  
     * @author LinBilin 
     * @param str 
     *            校驗手機字符串 
     * @return 返回true,否則爲false 
     */  
    public static boolean isPhoneNum(String str) {  
        // 如果字符串爲空,直接返回false  
        if (str == null || str.trim().equals("")) {  
            return false;  
        } else {  
            int comma = str.indexOf(",");// 是否含有逗號  
            int caesuraSign = str.indexOf("、");// 是否含有頓號  
            int space = str.trim().indexOf(" ");// 是否含有空格  
            if (comma == -1 && caesuraSign == -1 && space == -1) {  
                // 如果號碼不含分隔符,直接驗證  
                str=str.trim();  
                return (isPhoneCallNum(str) || isChinaTelecomPhoneNum(str)  
                        || isChinaUnicomPhoneNum(str) || isChinaMobilePhoneNum(str)) ? true  
                        : false;  
            } else {  
                // 號碼含分隔符,先把分隔符統一處理爲英文狀態下的逗號  
                if (caesuraSign != -1) {  
                    str=str.replaceAll("、", ",");  
                }  
                if (space != -1) {  
                    str=str.replaceAll(" ", ",");  
                }  
  
                String[] phoneNumArr = str.split(",");  
                //遍歷驗證  
                for (String temp : phoneNumArr) {  
                    temp=temp.trim();  
                    if (isPhoneCallNum(temp) || isChinaTelecomPhoneNum(temp)  
                            || isChinaUnicomPhoneNum(temp)  
                            || isChinaMobilePhoneNum(temp)) {  
                        continue;  
                    } else {  
                        return false;  
                    }  
                }  
                return true;  
            }  
  
        }  
  
    }  
      
    /** 
     * 執行正則表達式 
     *  
     * @param pat 
     *            表達式 
     * @param str 
     *            待驗證字符串 
     * @return 返回true,否則爲false 
     */  
    private static boolean match(String pat, String str) {  
        Pattern pattern = Pattern.compile(pat);  
        Matcher match = pattern.matcher(str);  
        return match.find();  
    }  
      
    public static void main(String[] args) {  
          
        System.out.println(isPhoneNum("17750581369"));  
        System.out.println(isPhoneNum("13306061248"));  
        System.out.println(isPhoneNum("17750581369,13306061248"));  
        System.out.println(isPhoneNum("17750581369 13306061248"));  
        System.out.println(isPhoneNum("17750581369、13306061248"));  
        System.out.println(isPhoneNum("0596-3370653"));  
  
    }  
  
}

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