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 cSystem.out.println("------------1--------------");System.out.println("a".matches("[abc]")); //trueSystem.out.println("z".matches("[abc]")); //falseSystem.out.println("ab".matches("[abc]")); //falseSystem.out.println("ab".matches("[abc][abc]")); //true//不能出现abcSystem.out.println("------------2--------------");System.out.println("a".matches("[^abc]")); //falseSystem.out.println("z".matches("[^abc]")); //trueSystem.out.println("zz".matches("[^abc]")); //falseSystem.out.println("zz".matches("[^abc][^abc]")); //true// a - z A - ZSystem.out.println("------------3--------------");System.out.println("a".matches("[a-zA-Z]")); //trueSystem.out.println("z".matches("[a-zA-Z]")); //trueSystem.out.println("aa".matches("[a-zA-Z]")); //falseSystem.out.println("zz".matches("[a-zA-Z]")); //falseSystem.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); //trueSystem.out.println("0".matches("[a-zA-Z]")); //falseSystem.out.println("0".matches("[a-zA-Z0-9]")); //true//[a-d[m-p]] a到d,或m到p 同 3System.out.println("------------4--------------");System.out.println("a".matches("[a-d[m-p]]")); //trueSystem.out.println("d".matches("[a-d[m-p]]")); //trueSystem.out.println("m".matches("[a-d[m-p]]")); //trueSystem.out.println("p".matches("[a-d[m-p]]")); //trueSystem.out.println("e".matches("[a-d[m-p]]"));//falseSystem.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]]")); //trueSystem.out.println("&".matches("[a-z&[def]]")); //trueSystem.out.println("d".matches("[a-z&&[def]]")); //trueSystem.out.println("0".matches("[a-z&&[def]]")); //falseSystem.out.println("a".matches("[a-z&&[def]]")); //falseSystem.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]]")); //falseSystem.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]]")); //trueSystem.out.println("m".matches("[a-z&&[^m-p]]")); //falseSystem.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("..")); //falseSystem.out.println("你a".matches("..")); //trueSystem.out.println("你".matches(".")); //true// \\d 只能是任意的一位数字System.out.println("a".matches("\\d")); //falseSystem.out.println("3".matches("\\d")); //trueSystem.out.println("333".matches("\\d")); //falseSystem.out.println("333".matches("\\d\\d\\d")); //true// \\w 只能是一位单词字符 [a-zA-Z_0-9] (可以是下划线)System.out.println("z".matches("\\w")); //trueSystem.out.println("2".matches("\\w")); //trueSystem.out.println("_".matches("\\w")); //trueSystem.out.println("21".matches("\\w")); //falseSystem.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,}")); //trueSystem.out.println("244".matches("\\w{6,}")); //false//必须是数字和字符 必须是4位System.out.println("23df".matches("[a-zA-Z0-9]{4}")); //trueSystem.out.println("23_f".matches("[a-zA-Z0-9]{4}")); //falseSystem.out.println("23df".matches("[\\w&&[^_]]{4}")); //trueSystem.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位任意数字,最后一位可以是数字可以是大写或小写的xString 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 总结
参考链接:
常用API-09-正则表达式初体验和基本规则_哔哩哔哩_bilibili
常用API-11-正则表达式基本练习2_哔哩哔哩_bilibili