Java正则验证手机号归属最全详解

前言

公司提出业务需要判断手机号码归属商,进过查找资料,自己验证,统计出如下号段和正则表达式,完美解决需求。

1、联通、移动、电信三大运营商枚举

package com.fintech.modules.base.enums;

/**
 * @Description: 运营商枚举
 * @author lc
 * @date 2018年4月10日
 */
public enum MobileOperEnum {

	UNICOM("UNICOM", "联通"),
	CMCC("CMCC", "移动"),
	TELECOM("TELECOM", "电信"),
	UNKNOWN("UNKNOWN","未知");  

    MobileOperEnum(String code, String name) {
        this.code = code;
        this.name = name;
    }

    private String code;
    private String name;

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}

2、具体验证类

package com.fintech.modules.base.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fintech.modules.base.enums.MobileOperEnum;

/**
 * 
 * @Description: 判断手机号所属运营商
 * @author lc
 * @date 2018年4月17日
 */
public class MobileLocationUtil {
	

	/**
	 * @Description: 判断输入的是否为数字
	 * @author lc
	 * @返回true说明是数字,false说明不全是数字
	 */
	private static boolean isNum(String phoneNum) {
		for (int i = 0; i < phoneNum.length(); i++) {
			if (!Character.isDigit(phoneNum.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @Description: 判断运营商
	 * @author lc
	 */
	public static String execute(String mobile, String cmccReg, String unicomReg, String telecomReg) {
		// 去除前后的空白
		mobile = mobile.trim();
		// 判断输入的电话号码是否合法
		if (mobile == null || mobile.length() < 11) {
			return MobileOperEnum.UNKNOWN.getCode();
		} else {
			// 处理国内的+86开头
			if (mobile.startsWith("+")) {
				mobile = mobile.substring(1);
			}
			if (mobile.startsWith("86")) {
				mobile = mobile.substring(2);
			}
		}
		// 去除+86后电话号码应为11位
		if (mobile.length() != 11) {
			return MobileOperEnum.UNKNOWN.getCode();
		}
		// 判断去除+86后剩余的是否全为数字
		if (!isNum(mobile)) {
			return MobileOperEnum.UNKNOWN.getCode();
		}
		if(cmccPrar(mobile, cmccReg)){
			return MobileOperEnum.CMCC.getCode();
		}
		if(unicomPrar(mobile, unicomReg)){
			return MobileOperEnum.UNICOM.getCode();
		}
		if(telecomPrar(mobile, telecomReg)){
			return MobileOperEnum.TELECOM.getCode();
		}
		return MobileOperEnum.UNKNOWN.getCode();
	}
	
	/**
	 * @Description: 移动号段
	 * @author lc
	 */
	public static Boolean cmccPrar(String mobile,String cmccReg){
		if(getPattern8(mobile, cmccReg) == true || getPattern7(mobile, cmccReg)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @Description: 联通号段
	 * @author lc
	 */
	public static Boolean unicomPrar(String mobile, String unicomReg){
		if(getPattern8(mobile, unicomReg) == true || getPattern7(mobile, unicomReg)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @Description: 电信号段
	 * @author lc
	 */
	public static Boolean telecomPrar(String mobile, String telecomReg){
		if(getPattern8(mobile, telecomReg) == true || getPattern7(mobile, telecomReg)){
			return true;
		}else{
			return false;
		}
	}
	
	
	public static Boolean getPattern8(String mobile, String reg){
		Pattern p = Pattern.compile("^"+reg+"\\d{8}$");  
		Matcher m = p.matcher(mobile);  
		return m.find();
	}
	
	public static Boolean getPattern7(String mobile, String reg){
		Pattern p = Pattern.compile("^"+reg+"\\d{7}$");  
		Matcher m = p.matcher(mobile);  
		return m.find();
	}
	

	public static void main(String[] args) {
                // 2018截止今天我敢说这是最全的手机号段
                String cmccReg = "((13[4-9])|(144)|(14[7-8])|(154)|(15[0-2,7-9])|(172)|(178)|(18[2-4,7-8])|(198)|(1703)|(1705)|(1706))";
		String unicomReg = "((13[0-2])|(140)|(14[5-6])|(15[5-6])|(166)|(171)|(17[5-6])|(18[5-6])|(1704)|(1707)|(1708)|(1709))";
		String telecomReg = "((133)|(141)|(149)|(153)|(173)|(177)|(18[0-1])|(189)|(199)|(1700)|(1701)|(1702))";
		String code = execute("17025400088", cmccReg, unicomReg, telecomReg);
		System.out.println(code);
	}
}
注意:由于我把正则表达式没有写死在代码,用读数据库的形式实现,所以正则表达式的前后都固定在代码,不然存在转义字符的问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十点摆码

有用你就打赏一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值