final String regex = "^([1-9]\\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (0?\\d|1\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$";
final String string = "2022-10-18 13:33:33";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
System.out.println(matcher.matches());
//或者使用
Pattern.matches(regex, string)
//或者使用
System.out.println(string.matches(regex));
提取汉字
str.replaceAll("\\s*","").replaceAll("[^(\\u4e00-\\u9fa5)]","")
提取字母
str.replaceAll("\\s*","").replaceAll("[^(A-Za-z)]","")
提取数字
str.replaceAll("\\s*","").replaceAll("[^(a-zA-Z0-9)]","")
数字+字母
str.replaceAll("\\s*","").replaceAll("[^(a-zA-Z0-9)]","")
过滤空字符串
//去掉首尾空格
System.out.println("去掉首尾空格:" + str1.trim());
//去掉所有空格
System.out.println("去掉所有空格:" + str2.replace(" ",""));
System.out.println("去掉所有空格:" + str3.replaceAll(" ",""));
System.out.println("去掉所有空格:" + str4.replaceAll(" +",""));
//去掉特殊的空格、制表符、换页符
System.out.println("去掉特殊的空格、制表符、换页符:" + str5.replaceAll("\\s*",""));
空字符串分割
\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
\f 匹配一个换页
\n匹配一个换行符
\r匹配一个回车符
\t匹配一个制表符
\v匹配一个垂直制表符
而“\s+”则表示匹配任意多个上面的字符。
另因为反斜杠在Java里是转义字符,所以在Java里,我们要这么用:str.split("\\s+");
[\S]表示,非空白就匹配
数字校验(包含负数)
-?[0-9]+(.[0-9]+)?
有效时间(00:00-23:59)
^(20|21|22|23|[0-1]\d):[0-5]\d-(20|21|22|23|[0-1]\d):[0-5]\d$
验证密码(大写字母、小写字母、数字、特殊字符中三种或三种以上的组合,长度自定)
String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W]+$)(?![a-z0-9]+$)(?![a-z\\W]+$)(?![0-9\\W]+$)[a-zA-Z0-9_\\W]*"
+ pwdThMin + "," + pwdThMax + "}$";
IP验证
^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$
端口验证
^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$
IP+端口验证
^((?:(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?:\.(?!\:)|)){4})\:(?!0)(\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$
MAC地址验证
^([0-9a-fA-F]{2})(([/\s:][0-9a-fA-F]{2}){5})$
与(|)
^([0-9a-fA-F]{2})(([/\s-][0-9a-fA-F]{2}){5})$
手机号验证
(\+\d+)?1[345789]\d{9}$
网址验证
((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)
JSON验证
/(?(DEFINE)
# Note that everything is atomic, JSON does not need backtracking if it's valid
# and this prevents catastrophic backtracking
(?(?>\s*(?&object)\s*|\s*(?&array)\s*))
(?
1-15 个字母数字登录名
^(?=.*[a-zA-Z]{1,})(?=.*[\d]{0,})[a-zA-Z0-9]{1,15}$
邮箱验证
^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$
替换文本中的日期
/**
* 替换文本中日期
*
* @param content 字符串
* @param date 替换的日期
* @return
*/
public String dateOfReplacement(String content, String date) {
Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})");
Matcher matcher = p.matcher(content);
while (matcher.find()) {
String group = matcher.group(0);
return content.replace(group, date);
}
return content;
}