0
点赞
收藏
分享

微信扫一扫

java-正则表达式判断手机号


要更加准确的匹配手机号码只匹配11位数字是不够的,比如说就没有以144开始的号码段,

  故先要整清楚现在已经开放了多少个号码段,国家号码段分配如下:

  移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188

  联通:130、131、132、152、155、156、185、186

  电信:133、153、180、189、(1349卫通)

  那么现在就可以正则匹配测试了,

  



1. import java.io.IOException;  
2.  
3. java.util.regex.Matcher;  
4.  
5. java.util.regex.Pattern;  
6.  
7. class ClassPathResource {  
8.  
9. static boolean isMobileNO(String mobiles){  
10.  
11. p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");  
12.  
13. m = p.matcher(mobiles);  
14.  
15.  
16.  
17. m.matches();  
18.  
19.  
20.  
21. static void main(String[] args) throws IOException {  
22.  
23.  
24.  
25.  
26.  
27.


import java.io.IOException;

  import java.util.regex.Matcher;

  import java.util.regex.Pattern;

  public class ClassPathResource {

  public static boolean isMobileNO(String mobiles){

  Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

  Matcher m = p.matcher(mobiles);

  System.out.println(m.matches()+"---");

  return m.matches();

  }

  public static void main(String[] args) throws IOException {

  System.out.println(ClassPathResource.isMobileNO("12016155153"));

  }

  }


第二种方法:

 



1. import java.util.regex.Matcher;  
2.  
3. java.util.regex.Pattern;  
4.  
5. value="手机号";  
6.  
7. regExp = "^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$";  
8.  
9. p = Pattern.compile(regExp);  
10.  
11. m = p.matcher(value);  
12.  
13. m.find();//boolean

举报

相关推荐

0 条评论