java中常见的正则表达式(更细节的表达式讲解在主页):
package com.feisi.test;
public class Test8 {
public static void main(String[] args) {
//1、匹配汉字:^[\\u4e00-\\u9fa5]{0,3}$
String str1 = "你好吗";
boolean b1 = str1.matches("^[\\u4e00-\\u9fa5]{0,3}$");
System.out.println(b1);//true
//2. 英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
//任选一种
String str2 = "cvhds15622";
boolean b2 = str2.matches("^[A-Za-z0-9]+$");
System.out.println(b2);//true
// 3.Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
String str3 = "1581632569@11.com";
boolean b3 = str3.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
System.out.println(b3);//true
//4.电话号码:^(\d{3,4})-\d{7,8}$
String str4 = "199-73994752";
boolean b4 = str4.matches("^(\\d{3,4})-\\d{7,8}$");
System.out.println(b4);//true
//5.手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
//任选一种方式
String str5 = "18873884752";
boolean b5 = str5.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$");
System.out.println(b5);//true
//6. 腾讯QQ号:[1-9][0-9]{4,} (腾讯QQ号从10000开始)
String str6 = "1887388";
boolean b6 = str6.matches("[1-9][0-9]{4,}");
System.out.println(b6);//true
// 7. 日期格式:^\d{4}-\d{1,2}-\d{1,2}
String str7 = "1999-10-24";
boolean b7 = str7.matches("^\\d{4}-\\d{1,2}-\\d{1,2}");
System.out.println(b7);//true
//8. 身份证号(15位、18位数字):^\d{15}|\d{18}$
String str8 = "575382325381235";
boolean b8 = str8.matches("^\\d{15}|\\d{18}$");
System.out.println(b8);//true
//9. 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
String str9 = "shi_1024";
boolean b9 = str9.matches("^[a-zA-Z][a-zA-Z0-9_]{4,15}$");
System.out.println(b9);//true
//10. 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$
String str10 = "shi_1024";
boolean b10 = str10.matches("^[a-zA-Z][a-zA-z0-9_]{5,17}$");
System.out.println(b10);//true
//11. 强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
String str11 = "Shi_1024";
boolean b11 = str11.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$");
System.out.println(b11);//true
}
}
最后附上史上最全的正则表达式:
https://www.cnblogs.com/fozero/p/7868687.html
//作业:登录界面