java 正则表达式工具类 (亲测有效)

package com.myProject.util;

import java.util.regex.Pattern;

/**
 * @ClassName: RegexUtil
 * @Author: wnn
 * @Description: 正则表达式工具类
 * @Date: 2021/9/1 16:28
 */
public class RegexUtil {
    /**
     * 身份证验证 15位或者18位身份证
     * @param  idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
     * @return true 匹配 ;false 不匹配
     */
    public static boolean checkIdCard(String idCard) {
        String regex = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        return Pattern.matches(regex,idCard);
    }

    /**
     *邮箱验证
     * @param email  匹配电子邮箱
     * @return true 匹配 ;false 不匹配
     */
    public static boolean checkEmail(String email) {
        String regex="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        return Pattern.matches(regex,email);
    }

    /**
     *电话号码验证  手机或者固话
     * @param phone  匹配电话
     * @return true 匹配 ;false 不匹配
     */
    public static boolean checkPhone(String phone) {
        String regexGH="^(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{8}$";//固话
        String regexSJ="^(86)*0*\\d{11}";//手机号
        return Pattern.matches(regexGH,phone)||Pattern.matches(regexSJ,phone);
    }

    /**
     *数字验证
     * @param num
     * @return true 匹配 ;false 不匹配
     */
    public static boolean checkNum(String num) {
        String regex="^\\d+$";
        return Pattern.matches(regex,num);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值