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);
    }

}