1 正则表达式作用
- 作用一:校验字符串是否满足规则
- 作用二:在一段文本中查找满足要求的内容
2 正则表达式规则
2.1 字符类
package com.bjpowernode.test14;
public class RegexDemo1 {
public static void main(String[] args) {
//public boolean matches(String regex): 判断是否与正则表达式匹配,匹配则返回 true
// 只能是 a b c
System.out.println("------------1--------------");
System.out.println("a".matches("[abc]")); //true
System.out.println("z".matches("[abc]")); //false
System.out.println("ab".matches("[abc]")); //false
System.out.println("ab".matches("[abc][abc]")); //true
//不能出现abc
System.out.println("------------2--------------");
System.out.println("a".matches("[^abc]")); //false
System.out.println("z".matches("[^abc]")); //true
System.out.println("zz".matches("[^abc]")); //false
System.out.println("zz".matches("[^abc][^abc]")); //true
// a - z A - Z
System.out.println("------------3--------------");
System.out.println("a".matches("[a-zA-Z]")); //true
System.out.println("z".matches("[a-zA-Z]")); //true
System.out.println("aa".matches("[a-zA-Z]")); //false
System.out.println("zz".matches("[a-zA-Z]")); //false
System.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); //true
System.out.println("0".matches("[a-zA-Z]")); //false
System.out.println("0".matches("[a-zA-Z0-9]")); //true
//[a-d[m-p]] a到d,或m到p 同 3
System.out.println("------------4--------------");
System.out.println("a".matches("[a-d[m-p]]")); //true
System.out.println("d".matches("[a-d[m-p]]")); //true
System.out.println("m".matches("[a-d[m-p]]")); //true
System.out.println("p".matches("[a-d[m-p]]")); //true
System.out.println("e".matches("[a-d[m-p]]"));//false
System.out.println("0".matches("[a-d[m-p]]"));//false
//[a-z&&[def]]a-z和def的交集。为:d,e,f
//细节:如果要求两个范围的交集,那么需要写符号&&
//如果写成了一个&,那么此时&表示就不是交集了,而是一个简简单单的&符号
System.out.println("------------5--------------");
System.out.println("a".matches("[a-z&[def]]")); //true
System.out.println("&".matches("[a-z&[def]]")); //true
System.out.println("d".matches("[a-z&&[def]]")); //true
System.out.println("0".matches("[a-z&&[def]]")); //false
System.out.println("a".matches("[a-z&&[def]]")); //false
System.out.println("&".matches("[a-z&&[def]]")); //false
//[a-z&&[^bc]] a-z和非bc的交集 (等同于[ad-z])
System.out.println("------------6--------------");
System.out.println("a".matches("[a-z&&[^bc]]"));
System.out.println("b".matches("[a-z&&[^bc]]")); //false
System.out.println("0".matches("[a-z&&[^bc]]")); //false
//[a-z&&[^m-p]] a到z和除了m到p的交集 (等同于[a-lq-z])
System.out.println("------------7--------------");
System.out.println("a".matches("[a-z&&[^m-p]]")); //true
System.out.println("m".matches("[a-z&&[^m-p]]")); //false
System.out.println("0".matches("[a-z&&[^m-p]]")); //false
}
}
2.2 预定义字符
package com.bjpowernode.test14;
public class RegexDemo2 {
public static void main(String[] args) {
// \ 转义字符改变后面那个字符原本的含义
//练习:以字符串的形式打印一个双引号
System.out.println("\"");
//此时\表示转义字符,改变了后面那个双引号原本的含义
//把他变成了一个普普通通的双引号而已。
// \ 表示转义字符
// 路径中的\\ :前面的\是一个转义字符,改变了后面\原本的含义,把他变成一个普普通通的\而己。
// .表示任意一个字符
System.out.println("你".matches("..")); //false
System.out.println("你a".matches("..")); //true
System.out.println("你".matches(".")); //true
// \\d 只能是任意的一位数字
System.out.println("a".matches("\\d")); //false
System.out.println("3".matches("\\d")); //true
System.out.println("333".matches("\\d")); //false
System.out.println("333".matches("\\d\\d\\d")); //true
// \\w 只能是一位单词字符 [a-zA-Z_0-9] (可以是下划线)
System.out.println("z".matches("\\w")); //true
System.out.println("2".matches("\\w")); //true
System.out.println("_".matches("\\w")); //true
System.out.println("21".matches("\\w")); //false
System.out.println("你".matches("\\w")); //false
// \\W 非单子字符 \\W 是对 \\w 的取反
System.out.println("你".matches("\\W")); //true
}
}
2.3 数量词
package com.bjpowernode.test14;
public class RegexDemo3 {
public static void main(String[] args) {
//必须是数字 字母 下划线 至少6位
System.out.println("2442f2_fsf".matches("\\w{6,}")); //true
System.out.println("244".matches("\\w{6,}")); //false
//必须是数字和字符 必须是4位
System.out.println("23df".matches("[a-zA-Z0-9]{4}")); //true
System.out.println("23_f".matches("[a-zA-Z0-9]{4}")); //false
System.out.println("23df".matches("[\\w&&[^_]]{4}")); //true
System.out.println("23_f".matches("[\\w&&[^_]]{4}")); //false
}
}
3 正则表达式插件
4 练习
需求
请编写正则表达式验证用户名是否满足要求。
要求:大小写字母,数字,下划线一共4-16位
请编写正则表达式验证身份证号码是否满足要求。
简单要求:18位,前17位任意数字,最后一位可以是数字可以是大写或小写的x
复杂要求:按照身份证号码的格式严格要求。
package com.bjpowernode.test14;
public class RegexDemo5 {
public static void main(String[] args) {
//用户名要求:大小写字母,数字,下划线一共4-16位
String regex1 = "\\w{4,16}";
System.out.println("zhangsan".matches(regex1));
System.out.println("$123".matches(regex1));
//身份证号码的简单校验:
//18位,前17位任意数字,最后一位可以是数字可以是大写或小写的x
String regex2 = "[1-9]\\d{16}(\\d|x|X)";
//或者写成 String regex2 = "[1-9]\\d{16}[\\dxX]";
System.out.println("15040119810705387X".matches(regex2));
//忽略大小写的书写方式
//在匹配的时候忽略bc的大小写
String regex3 = "a(?i)bc";
System.out.println("aBC".matches(regex3)); //true
}
}
5 总结
参考链接: