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);
	}
}
注意:由于我把正则表达式没有写死在代码,用读数据库的形式实现,所以正则表达式的前后都固定在代码,不然存在转义字符的问题。
<think>好的,用户想要查找用于验证手机号Java正则表达式代码示例。首先,我需要回忆一下中国的手机号格式。通常,手机号以1开头,第二位是3-9之间的数字,后面跟着9位数字,总共11位。因此,正则表达式应该符合这个模式。 接下来,我需要检查提供的引用内容。引用[2]、[3]和[5]都提到了手机号验证的相关内容。特别是引用[3]提供了一个完整的示例代码,其中正则表达式是^1[3-9]\\d{9}$。这个模式应该可以正确匹配有效的手机号码。 然后,我要确保正则表达式Java中的正确使用。Java中使用Pattern和Matcher类来处理正则表达式,如引用[3]中的代码所示。需要注意转义字符的使用,比如在Java字符串中,反斜杠需要写成两个反斜杠,所以正则表达式里的\d应该写成\\d。 另外,引用[4]提到了一些正则表达式的基础,比如转义字符和字符类的使用,这有助于理解正则表达式的构造。例如,使用^和$来确保整个字符串匹配,而不是部分匹配。 最后,整合这些信息,提供一个清晰的代码示例,并解释每个部分的作用。同时,需要生成相关问题,帮助用户进一步了解正则表达式的其他应用或优化方法。</think>以下是Java验证手机号正则表达式实现步骤及完整示例: ### 手机号验证正则表达式实现步骤 1. **手机号格式规范**: - 以数字1开头 - 第二位数字为3-9 - 总长度11位 - 正则表达式模式:`^1[3-9]\d{9}$`[^3][^5] 2. **Java代码实现**: ```java import java.util.regex.Pattern; import java.util.regex.Matcher; public class PhoneValidator { public static boolean validatePhone(String phone) { String regex = "^1[3-9]\\d{9}$"; // 正则表达式 Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(phone); return matcher.matches(); } public static void main(String[] args) { String[] testNumbers = {"13812345678", "12700001111", "1581234abc"}; for (String num : testNumbers) { System.out.println(num + ":" + (validatePhone(num) ? "有效" : "无效")); } } } ``` ### 关键代码说明 1. **正则表达式分解**: - `^`:字符串开始标识 - `1`:必须1开头 - `[3-9]`:第二位限制3-9 - `\\d{9}`:匹配9位数字(Java需双反斜杠转义) - `$`:字符串结束标识[^5] 2. **模式验证方法**: - 使用`Pattern.compile()`预编译正则表达式提升性能[^1] - `matcher.matches()`进行完整匹配验证[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

十点摆码

有用你就打赏一下

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

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

打赏作者

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

抵扣说明:

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

余额充值