基础语法可以看我另一篇博客:正则表达式【规则】【实例】【技巧】_正则规则-CSDN博客
输出结果全是true
public class StringRegexTest {public static void main(String[] args) {System.out.println(matchSingleNum("1"));System.out.println(matchMultiNum("123"));System.out.println(matchSingleLetter("a"));System.out.println(matchMultiLetter("abc"));System.out.println(matchLetterOrNum("abc123abc"));System.out.println(matchAppointLength("abc12abc", 8));System.out.println(matchEmail("1423sdaf23@16sdf.com"));System.out.println(matchURL("https://blog.csdn.net/lklalmq/article/details/136012146?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522bb9e340f647a3193fa75a3234c345784%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=bb9e340f647a3193fa75a3234c345784&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-136012146-null-null.nonecase&utm_term=%E6%AD%A3%E5%88%99&spm=1018.2226.3001.4450"));System.out.println(matchEmpty(" "));System.out.println(matchNotEmpty("是"));System.out.println(matchStart("Hello World", "Hello"));System.out.println(matchEnd("Hello World", "World"));System.out.println(matchContains("Hello World", "o W"));System.out.println(matchContainsIgnoreCase("Hello World", "O w"));System.out.println(matchPhone("13232323232"));System.out.println(matchDateTime("2024-01-01 12:00:00"));System.out.println(matchLocalDateTime(LocalDateTime.now().toString()));System.out.println(matchRgbColor("#F02EAF"));System.out.println(matchNotContains("123abc123ab123", "4"));System.out.println(matchRepeat("abbc"));System.out.println(matchMid("Hello World", "llo"));System.out.println(matchWord("&%d break 23"));System.out.println(matchOptional("Hello World", "World"));System.out.println(matchIdCard("332613202302291982"));System.out.println(matchWeakPassword("qwe123"));System.out.println(matchMidPassword("qweASD123"));System.out.println(matchStrongPassword("^&*daf123*&Ss"));System.out.println(matchFileName("application.properties"));System.out.println(matchIPv4("000.000.000.000"));System.out.println(matchChinese("黄小桃"));}// 匹配一个数字public static Boolean matchSingleNum(String str){String regex = "\\d";return str.matches(regex);}// 匹配多个连续数字public static Boolean matchMultiNum(String str){String regex = "\\d+";return str.matches(regex);}// 匹配一个字母,不区分大小写public static Boolean matchSingleLetter(String str){String regex = "[a-zA-Z]";return str.matches(regex);}// 匹配多个字母,不区分大小写public static Boolean matchMultiLetter(String str){String regex = "[a-zA-Z]+";return str.matches(regex);}// 匹配多个字母或数字public static Boolean matchLetterOrNum(String str){String regex = "[a-zA-Z0-9]+";return str.matches(regex);}// 匹配特定长度的字符串public static Boolean matchAppointLength(String str, int length){String regex = ".{" + length + "}";return str.matches(regex);}// 匹配基本的邮箱地址// 邮箱的格式:用户名@域名// 用户名:一般6-18位,由大小写字母、数字、下划线、点、加减号、百分号组成// 二级域名:至少1位,由大小写字母、数字、点、连字符组成// 顶级域名:至少2位,由大小写字符组成,当然也有很多新的顶级域名可以是数字public static Boolean matchEmail(String str){String regex = "[\\w.%+-]{6,18}@[a-zA-Z0-9.-]+\\.[a-zA-Z0-9]{2,}";return str.matches(regex);}// 匹配URL// URL的格式:协议://域名/路径?参数// 协议:http或https// 二级域名:至少1位,由大小写字母、数字、点、连字符组成// 顶级域名:至少两位,由大小写字符、数字组成// 路径:可选,以 / 开头,由除了\n之外的任意字符组成,也可能位空// 参数:可选,以 ? 开头,由大小写字母、数字、+-._%&=组成,也可能为空// 注意:正则表达式中,\n是匹配换行符的,所以在路径中不能出现\n\r\f等空白字符public static Boolean matchURL(String str){String regex = "(http://|https://)[a-zA-Z0-9.-]+\\.[a-zA-Z0-9]{2,}(/.*)*(\\?[\\w.%&+-=]*)?";return str.matches(regex);}// 匹配一个空白字符,包括空格、制表符、换行符、回车符、换页符等public static Boolean matchEmpty(String str){String regex = "[\\x20\\t\\n\\r\\f]";return str.matches(regex);}// 匹配非空字符public static Boolean matchNotEmpty(String str){String regex = "[^\\s]"; // 或者\\Sreturn str.matches(regex);}// 匹配字符串开头public static Boolean matchStart(String str, String start){String regex = "^" + start + ".*";return str.matches(regex);}// 匹配字符串结尾public static Boolean matchEnd(String str, String end){String regex = ".*" + end + "$";return str.matches(regex);}// 匹配包含特定子串public static Boolean matchContains(String str, String mid){String regex = ".*" + mid + ".*";return str.matches(regex);}// 匹配包含特定子串,不区分大小写// (?i) 标志,表示忽略大小写,需要配合Pattern类、Matcher类使用public static Boolean matchContainsIgnoreCase(String str, String mid){String regex = "(?i).*" + mid + ".*";Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(str);return matcher.find();}// 匹配电话号码// 御三家电话号码的格式:1XX-XXXX- XXXX// 分为三段,第一段的前三位是网络识别号,以1开头,第二位3 - 9// 第二段是四位地区编号,第三段是四位用户号码public static Boolean matchPhone(String str){String regex = "1[3-9]\\d{9}";return str.matches(regex);}// 匹配日期时间// 日期时间的格式:YYYY-MM-DD HH:MM:SSpublic static Boolean matchDateTime(String str){String regex = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";return str.matches(regex);}// 匹配java的LocalDateTime.now().toString()生成的时间字符串// 格式:YYYY-MM-DDTHH:mm:ss.SSSpublic static Boolean matchLocalDateTime(String str){String regex = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}";return str.matches(regex);}// 匹配十六进制RGB格式颜色// 格式:#RRGGBB 或 #RGBpublic static Boolean matchRgbColor(String str){String regex = "#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})";return str.matches(regex);}// 匹配不包含特定字符的字符串public static Boolean matchNotContains(String str, String notContains){String regex = ".*(?!" + notContains + ")*.*";return str.matches(regex);}// 匹配重复两次的字符public static Boolean matchRepeat(String str){String regex = ".*(.)\\1.*";return str.matches(regex);}// 匹配字符中间的字符,不包括首尾public static Boolean matchMid(String str, String mid){String regex = ".+" + mid + ".+";return str.matches(regex);}// 匹配一个单词public static Boolean matchWord(String str){String regex = ".*\\b[a-zA-Z]+\\b.*";return str.matches(regex);}// 匹配以特定字符开头和结尾的字符串public static Boolean matchStartAndEnd(String str, String start, String end){String regex = Pattern.quote(start) + ".*" + Pattern.quote(end);return str.matches(regex);}// 匹配可选字符public static Boolean matchOptional(String str, String optional){String regex = "Hello " + "(" + optional + ")?"; // 或者 "Hello(?:" + optional + ")"return str.matches(regex);}// 匹配身份证号// 格式:地址码6位 + 出生日期8位 + 顺序码3位 + 校验码1位// 地址码:省份代码2位 + 城市代码2位 + 区县代码2位// 顺序码:区分同一天内出生的人,奇数代表男性,偶数代表女性// 校验码:根据前17位通过特定算法计算得到,校验身份证号的正确性// 首位不能为0,二代身份证号18位是从1999年10月1号开始的,一代更早,15位// 不考虑平年闰年导致的2月份28天还是29天的问题public static Boolean matchIdCard(String str){String regex = "[1-9]\\d{5}(19|20)\\d{2}((0[13456789]|1[0-2])(0[1-9]|[12]\\d|30|31)|(02(0[1-9]|1\\d|2[0-9])))\\d{3}[\\dXx]";return str.matches(regex);}// 匹配银行卡号public static Boolean matchBankCard(String str){String regex = "\\d{13,19}";return str.matches(regex);}// 匹配弱密码// 长度 < 8,只包含数字,纯小写或者纯大写字母public static Boolean matchWeakPassword(String str){String regex = "[a-z0-9]|[a-z0-9]{0,7}";return str.matches(regex);}// 匹配中等密码// 长度 >= 8,至少包含一个大写字母、一个小写字母,一个数字public static Boolean matchMidPassword(String str){String regex = "(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)[a-zA-Z0-9]{8,}";return str.matches(regex);}// 匹配强密码// 长度 >= 8, 至少包含一个大写字母,一个小写字母,一个数字,一个特殊字符public static Boolean matchStrongPassword(String str){String regex = "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()\\-+_=])[A-Za-z\\d!@#$%^&*()\\-+_=]{8,}";return str.matches(regex);}// 匹配XML、JSON、YAML、HTML、CSS、JS、Properties等文件名// 格式:文件名.后缀名// 文件名:不能包含\ / : * ? " < > |public static Boolean matchFileName(String str){String regex = "[^\\/:*?\"<>|]+\\.[a-zA-Z0-9]+";return str.matches(regex);}// 匹配IPv4地址// 格式:0.0.0.0 - 255.255.255.255// 如果格式为 000.000.000.000,- 255.255.255.255// 则regex应该这么写:// (25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)public static Boolean matchIPv4(String str){String regex = "([0-1]?[0-9]?[0-9]|2[0-4]\\d|25[0-5])(\\.([0-1]?[0-9]?[0-9]|2[0-4]\\d|25[0-5])){3}";return str.matches(regex);}// 匹配中文字符// Unicode的汉字编码范围:U+4E00 - U+9FA5public static Boolean matchChinese(String str){String regex = "[\\u4e00-\\u9fa5]+";return str.matches(regex);}
}