1.校验E-Mail 地址

String str = "Swera@xsoxab.net";
// 邮箱验证规则\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}
String regEx = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);
2.校验密码强度
密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。
String str = "21212bAqsa";
// 密码强度验证规则
String regEx = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
 boolean rs = matcher.matches();
System.out.println(rs);
3.校验网址URL
String str = "http://www.runoob.com/java/java-regular-expressions.html";
// 网址URL验证规则^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+
String regEx = "^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);
4.校验金额,保留2位小数
String str = "5621212.23";
// 网址URL验证规则
String regEx = "^[0-9]+(.[0-9]{2})?$";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);
5. 由数字、26个英文字母或下划线组成的字符串
String str = "_s621212a23_";
// 由数字、26个英文字母或下划线组成的字符串验证规则
String regEx = "^\\w+$";
// 编译正则表达式
Pattern pattern = Pattern.compile(regEx);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// 字符串是否与正则表达式相匹配
boolean rs = matcher.matches();
System.out.println(rs);


更多推荐