手机号码校验工具类(正则表达式)

手机号码有很多种方式,可以使用正则表达式,进行判断。网上有很多,不用去记,查就行。
java 复制代码
import org.apache.commons.lang3.StringUtils;
​
import java.util.regex.Matcher;
import java.util.regex.Pattern;
​
/**
 * 手机号码校验类 (使用正则表达式)
 *
 * @author Z
 * @date 2023/9/27 21:45
 */
public class ValidatorUtil {
​
    private static final Pattern mobile_pattern = Pattern.compile("[1]([3-9])[0-9]{9}$");
​
    public static boolean isMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)){
            return false;
        }
        Matcher matcher = mobile_pattern.matcher(mobile);
        return matcher.matches();
    }
}
java 复制代码
service类调用这个工具类:ValidatorUtil.isMobile(value) 用来判断手机号码格式正确与否

Pattern 表示一个编译后的正则表达式模式 ,它可以被用来创建Matcher对象

Matcher 提供了对字符串进行正则匹配的功能。

通过使用**Pattern** 类的**compile()** 方法,可以将一个正则表达式字符串编译为一个Pattern对象 。然后,可以使用该对象创建一个Matcher对象,通过调用**Matcher** 对象的方法进行字符串的匹配操作。这两个类通常是一起使用。

上面是用于验证手机号码的正则表达式模式,它的格式要求如下:

  • 以数字1开头,表示中国的手机号码;

  • 第二位数字必须是3到9之间的任意数字;

  • 后面跟着9个数字字符。

通过使用这个正则表达式模式,可以判断一个字符串是否符合中国手机号码的格式要求。

下面就是手机号验证的最新正则表达式

java 复制代码
/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
相关推荐
q***57501 天前
问题:Flask应用中的用户会话(Session)管理失效
后端·python·flask
清风25561 天前
文件下载图片下载
运维·后端
薛定谔的猫19821 天前
docker 安装redis
java·spring·mybatis
q***42821 天前
解决bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException
java·数据库·sql
q***D4431 天前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
s***55811 天前
SpringBoot整合JWT
java·spring boot·后端
3***16101 天前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
p***92481 天前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
K***72841 天前
将 vue3 项目打包后部署在 springboot 项目运行
java·spring boot·后端
0***86331 天前
SpringBoot接口防抖(防重复提交),接口幂等性,轻松搞定
java·spring boot·后端