1、正则表达式:
- 字符类演示:
package cn.njupt;
public class ArrayReverse {
public static void main(String[] args) {
String regex1 = "[abc]"; &&& 代表单个字符
System.out.println("a".matches(regex1)); &&& true
System.out.println("*".matches(regex1)); &&& false
String regex2 = "[^abc]"; &&& 代表单个字符,除了abc
System.out.println("a".matches(regex2)); &&& false
System.out.println("*".matches(regex2)); &&& true
System.out.println("10".matches(regex2)); &&& false (单个字符,这个10是两个字符)
String regex3 = "[a-zA-Z]"; &&& 代表单个字符,a-z或A-Z,两端包括在内
System.out.println("a".matches(regex3)); &&& true
System.out.println("z".matches(regex3)); &&& true
System.out.println("&".matches(regex3)); &&& false
String regex4 = "[a-d[m-p]]"; &&& 代表单个字符,a-d和m-p的并集
System.out.println("a".matches(regex4)); &&& true
System.out.println("z".matches(regex4)); &&& false
System.out.println("n".matches(regex4)); &&& true
String regex5 = "[a-z&&[def]]"; &&& 代表单个字符,a-z和def的交集,就相当于[def]
String regex6 = "[a-z&&[^bc]]"; &&& 代表单个字符,a-z除了bc,相当于[ad-z]
System.out.println("a".matches(regex6)); &&& true
System.out.println("b".matches(regex6)); &&& false
String regex7 = "[a-z&&[^m-p]]"; &&& 代表单个字符,a-z除了m-p,相当于[a-lq-z]
}
}
- 预定义字符类演示:
package cn.njupt;
public class ArrayReverse {
public static void main(String[] args) {
String regex1 = "."; &&& 代表单个字符,要是想代表两个就".."
System.out.println("a".matches(regex1)); &&& true
System.out.println("ab".matches(regex1)); &&& false
String regex2 = "\\d"; &&& 同[0-9],单个数字("\d",第一个是转义字符"\")
System.out.println("0".matches(regex2)); &&& true
System.out.println("12".matches(regex2)); &&& false(单个数字,这个12是两个字符)
System.out.println("a".matches(regex2)); &&& false
String regex3 = "\\D"; &&& 同[^0-9],单个非数字
System.out.println("a".matches(regex3)); &&& true
System.out.println("0".matches(regex3)); &&& false
System.out.println("9".matches(regex3)); &&& false
String regex4 = "\\s"; &&& 空白字符,相当于[ \t\n\x0B\f\r]
System.out.println(" ".matches(regex4)); &&& true
System.out.println(" ".matches(regex4)); &&& true,制表符\t
System.out.println(" ".matches(regex4)); &&& false 四个空格,只能是单个字符
String regex5 = "\\S"; &&& 非空白字符,相当于[^\s]
System.out.println(" ".matches(regex5)); &&& false
System.out.println(" ".matches(regex5)); &&& false,制表符\t
System.out.println(" ".matches(regex5)); &&& false 四个空格,只能是单个字符
System.out.println("a".matches(regex5)); &&& true
String regex6 = "\\w"; &&& [a-zA-Z_0-9]单个字符(注意,还有一个_)
System.out.println("a".matches(regex6)); &&& true
System.out.println("&".matches(regex6)); &&& false
System.out.println("_".matches(regex6)); &&& true
String regex7 = "\\W"; &&& [^\w]取反
}
}
- Greedy quantifiers数量词:
package cn.njupt;
public class ArrayReverse {
public static void main(String[] args) {
String regex1 = "[abc]?"; &&& 前面的字符1次或者1次也没有
System.out.println("a".matches(regex1)); &&& true 1次
System.out.println("".matches(regex1)); &&& true 1次也没有
System.out.println("d".matches(regex1)); &&& false(就算没有,也不能是别的)
String regex2 = "[abc]*"; &&& 0次或多次(包括1次)
System.out.println("".matches(regex2)); &&& true 0次
System.out.println("a".matches(regex2)); &&& true 1次
System.out.println("aaa".matches(regex2)); &&& true 多次
String regex3 = "[abc]+"; &&& 1次或多次
String regex4 = "[abc]{5}"; &&& 恰好5次
System.out.println("aaaab".matches(regex4)); &&& true
System.out.println("aaaaaaaaaaaaaaaa".matches(regex4)); &&& false
System.out.println("a".matches(regex4)); &&& false
String regex5 = "[abc]{5,}"; &&& 至少5次
String regex6 = "[abc]{5,10}"; &&& 至少5次,但不超过10次(包含头尾)
}
}