首页 > 技术文章 > java-正则表达式判断移动联通电信手机号

itcqx 2016-08-02 11:43 原文

  1.   1 package com.linbilin.phone;  
      2   
      3 import java.util.regex.Matcher;  
      4 import java.util.regex.Pattern;  
      5   
      6 public class CheckPhone {  
      7   
      8     /** 电话格式验证 **/  
      9     private static final String PHONE_CALL_PATTERN = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}(-\\d{1,4})?$";  
     10   
     11     /** 
     12      * 中国电信号码格式验证 手机段: 133,153,180,181,189,177,1700 
     13      * **/  
     14     private static final String CHINA_TELECOM_PATTERN = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";  
     15   
     16     /** 
     17      * 中国联通号码格式验证 手机段:130,131,132,155,156,185,186,145,176,1709 
     18      * **/  
     19     private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";  
     20   
     21     /** 
     22      * 中国移动号码格式验证 
     23      * 手机段:134,135,136,137,138,139,150,151,152,157,158,159,182,183,184 
     24      * ,187,188,147,178,1705 
     25      * **/  
     26     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}$)";  
     27     /** 
     28      * 验证电话号码的格式 
     29      *  
     30      * @author LinBilin 
     31      * @param str 
     32      *            校验电话字符串 
     33      * @return 返回true,否则为false 
     34      */  
     35     public static boolean isPhoneCallNum(String str) {  
     36   
     37         return str == null || str.trim().equals("") ? false : match(  
     38                 PHONE_CALL_PATTERN, str);  
     39     }  
     40   
     41     /** 
     42      * 验证【电信】手机号码的格式 
     43      *  
     44      * @author LinBilin 
     45      * @param str 
     46      *            校验手机字符串 
     47      * @return 返回true,否则为false 
     48      */  
     49     public static boolean isChinaTelecomPhoneNum(String str) {  
     50   
     51         return str == null || str.trim().equals("") ? false : match(  
     52                 CHINA_TELECOM_PATTERN, str);  
     53     }  
     54   
     55     /** 
     56      * 验证【联通】手机号码的格式 
     57      *  
     58      * @author LinBilin 
     59      * @param str 
     60      *            校验手机字符串 
     61      * @return 返回true,否则为false 
     62      */  
     63     public static boolean isChinaUnicomPhoneNum(String str) {  
     64   
     65         return str == null || str.trim().equals("") ? false : match(  
     66                 CHINA_UNICOM_PATTERN, str);  
     67     }  
     68   
     69     /** 
     70      * 验证【移动】手机号码的格式 
     71      *  
     72      * @author LinBilin 
     73      * @param str 
     74      *            校验手机字符串 
     75      * @return 返回true,否则为false 
     76      */  
     77     public static boolean isChinaMobilePhoneNum(String str) {  
     78   
     79         return str == null || str.trim().equals("") ? false : match(  
     80                 CHINA_MOBILE_PATTERN, str);  
     81     }  
     82       
     83   
     84     /** 
     85      * 验证手机和电话号码的格式 
     86      *  
     87      * @author LinBilin 
     88      * @param str 
     89      *            校验手机字符串 
     90      * @return 返回true,否则为false 
     91      */  
     92     public static boolean isPhoneNum(String str) {  
     93         // 如果字符串为空,直接返回false  
     94         if (str == null || str.trim().equals("")) {  
     95             return false;  
     96         } else {  
     97             int comma = str.indexOf(",");// 是否含有逗号  
     98             int caesuraSign = str.indexOf("、");// 是否含有顿号  
     99             int space = str.trim().indexOf(" ");// 是否含有空格  
    100             if (comma == -1 && caesuraSign == -1 && space == -1) {  
    101                 // 如果号码不含分隔符,直接验证  
    102                 str=str.trim();  
    103                 return (isPhoneCallNum(str) || isChinaTelecomPhoneNum(str)  
    104                         || isChinaUnicomPhoneNum(str) || isChinaMobilePhoneNum(str)) ? true  
    105                         : false;  
    106             } else {  
    107                 // 号码含分隔符,先把分隔符统一处理为英文状态下的逗号  
    108                 if (caesuraSign != -1) {  
    109                     str=str.replaceAll("、", ",");  
    110                 }  
    111                 if (space != -1) {  
    112                     str=str.replaceAll(" ", ",");  
    113                 }  
    114   
    115                 String[] phoneNumArr = str.split(",");  
    116                 //遍历验证  
    117                 for (String temp : phoneNumArr) {  
    118                     temp=temp.trim();  
    119                     if (isPhoneCallNum(temp) || isChinaTelecomPhoneNum(temp)  
    120                             || isChinaUnicomPhoneNum(temp)  
    121                             || isChinaMobilePhoneNum(temp)) {  
    122                         continue;  
    123                     } else {  
    124                         return false;  
    125                     }  
    126                 }  
    127                 return true;  
    128             }  
    129   
    130         }  
    131   
    132     }  
    133       
    134     /** 
    135      * 执行正则表达式 
    136      *  
    137      * @param pat 
    138      *            表达式 
    139      * @param str 
    140      *            待验证字符串 
    141      * @return 返回true,否则为false 
    142      */  
    143     private static boolean match(String pat, String str) {  
    144         Pattern pattern = Pattern.compile(pat);  
    145         Matcher match = pattern.matcher(str);  
    146         return match.find();  
    147     }  
    148       
    149     public static void main(String[] args) {  
    150           
    151         System.out.println(isPhoneNum("17750581369"));  
    152         System.out.println(isPhoneNum("13306061248"));  
    153         System.out.println(isPhoneNum("17750581369,13306061248"));  
    154         System.out.println(isPhoneNum("17750581369 13306061248"));  
    155         System.out.println(isPhoneNum("17750581369、13306061248"));  
    156         System.out.println(isPhoneNum("0596-3370653"));  
    157   
    158     }  
    159   
    160 }  

     

推荐阅读