103 lines
3.6 KiB
Java
103 lines
3.6 KiB
Java
package com.imyeyu.utils;
|
||
|
||
import com.imyeyu.java.TimiJava;
|
||
|
||
import java.util.regex.Pattern;
|
||
|
||
/**
|
||
* @author 夜雨
|
||
* @since 2025-10-30 17:16
|
||
*/
|
||
public class Regex {
|
||
|
||
/** 手机号码 (11 位,以 1 开头) */
|
||
public static final String MOBILE_PHONE = "^1[3-9]\\d{9}$";
|
||
|
||
/** 固定电话号码 (带区号,如:010-12345678 或 0512-1234567) */
|
||
public static final String FIXED_PHONE = "^(0\\d{2,3}-)?[1-9]\\d{6,7}$";
|
||
|
||
/** 身份证号码 (15 位或 18 位) */
|
||
public static final String ID_CARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)";
|
||
|
||
/** 邮箱地址 */
|
||
public static final String EMAIL = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
|
||
|
||
/** 中文姓名 (2-10 个中文字符) */
|
||
public static final String CHINESE_NAME = "^[\\u4e00-\\u9fa5]{2,10}$";
|
||
|
||
/** 邮政编码 (6 位数字) */
|
||
public static final String POSTAL_CODE = "^[1-9]\\d{5}$";
|
||
|
||
/** 银行卡号 (16-19 位数字) */
|
||
public static final String BANK_CARD = "^[1-9]\\d{15,18}$";
|
||
|
||
/** QQ号码 (5-12 位数字) */
|
||
public static final String QQ_NUMBER = "^[1-9]\\d{4,11}$";
|
||
|
||
/** 微信号 (6-20 位字母、数字、下划线、减号) */
|
||
public static final String WECHAT_ID = "^[a-zA-Z][-_a-zA-Z0-9]{5,19}$";
|
||
|
||
/** 车牌号码 (包含新能源车牌) */
|
||
public static final String LICENSE_PLATE = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}[A-Z]{1}[A-Z0-9]{4,5}[A-Z0-9挂学警港澳]{1}$";
|
||
|
||
/** 日期格式 (yyyy-MM-dd) */
|
||
public static final String DATE = "^\\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[12]\\d|3[01])$";
|
||
|
||
/** 时间格式 (HH:mm:ss) */
|
||
public static final String TIME = "^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$";
|
||
|
||
/** 金额 (支持小数,最多两位小数) */
|
||
public static final String MONEY = "^[0-9]+(\\.[0-9]{1,2})?$";
|
||
|
||
/** 用户名 (4-16 位字母、数字、下划线) */
|
||
public static final String USERNAME = "^[a-zA-Z0-9_]{4,16}$";
|
||
|
||
/** 密码 (6-18 位,包含字母和数字) */
|
||
public static final String PASSWORD = "^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z0-9]{6,18}$";
|
||
|
||
/** 中文字符 */
|
||
public static final String CHINESE_CHAR = "^[\\u4e00-\\u9fa5]+$";
|
||
|
||
/** 网址 */
|
||
public static final String URL = "^(https?://)?([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
|
||
|
||
/** IP 地址 */
|
||
public static final String IP_ADDRESS = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$";
|
||
|
||
/** 统一社会信用代码 */
|
||
public static final String UNIFIED_SOCIAL_CREDIT_CODE = "^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$";
|
||
|
||
/** 港澳居民来往内地通行证 */
|
||
public static final String HONG_KONG_MACAO_PASS = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$";
|
||
|
||
/** 台湾居民来往大陆通行证 */
|
||
public static final String TAIWAN_PASS = "^[0-9]{8}|[0-9]{10}$";
|
||
|
||
/** 护照 */
|
||
public static final String PASSPORT = "^[a-zA-Z0-9]{5,17}$";
|
||
|
||
// IPv4 正则表达式
|
||
public static final String IPv4 = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
||
|
||
// IPv6 正则表达式(简化版)
|
||
public static final String IPv6 = "^([0-9a-fA-F]{0,4}:){7}[0-9a-fA-F]{0,4}$|^::1$|^::$|^([0-9a-fA-F]{0,4}:){1,7}:$";
|
||
|
||
// 域名正则表达式(RFC 1123)
|
||
public static final String DOMAIN = "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)*[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?$";
|
||
|
||
public static boolean isMatch(String regex, String text) {
|
||
if (TimiJava.isEmpty(regex) || TimiJava.isEmpty(text)) {
|
||
return false;
|
||
}
|
||
try {
|
||
return Pattern.matches(regex, text);
|
||
} catch (Exception e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static boolean isNotMatch(String regex, String text) {
|
||
return !isMatch(regex, text);
|
||
}
|
||
}
|