- 正则表达式的作用:校验字符串是否满足规则,在一段文本中查找满足条件的内容
 
- 正则表达式从左到右依次匹配
 
- \: 表示转义字符    改变后面那个字符的原本含义,简单来记忆,两个\表示一个\
 
 
- 判断单个字符:
 
 
- 例子:
 //只能是a b  c
        System.out.println("a".matches("[abc]"));//true
        System.out.println("b".matches("[abc]"));//true
        System.out.println("ab".matches("[abc]"));//false
        System.out.println("ab".matches("[abc][abc]"));//true
        //不能出现  a  b  c
        System.out.println("a".matches("[^abc]"));//false
        System.out.println("d".matches("[^abc]"));//true
        System.out.println("ab".matches("[^abc]"));//false
        System.out.println("de".matches("[^abc][^abc]"));//true
        //a-z  A-Z(包括头尾的范围)   其中-表示范围
        System.out.println("a".matches("[a-zA-Z]"));//true
        System.out.println("ab".matches("[a-zA-Z]"));//false
        System.out.println("ab".matches("[a-zA-Z][a-zA-Z]"));//true
        System.out.println("0".matches("[a-zA-Z0-9]"));//true
        System.out.println("0".matches("[0-9]"));//true
//[a-b[m-p]]   表示a-d 或则m-p
        System.out.println("a".matches("[a-b[m-p]]"));//true
        System.out.println("ab".matches("[a-b[m-p]]"));//false
        System.out.println("0".matches("[a-b[m-p]]"));//false
//[a-z&&[def]]  表示a--z和def的交集  就是def
        //细节:如果要求两个范围的交集,则要写&&,如果只写了一个,则只是表示一个&的符号而已
        System.out.println("a".matches("[a-z&&[def]]"));//false
        System.out.println("&".matches("[a-z&[d-f]]"));//true
        System.out.println("d".matches("[a-z&&[d-f]]"));//true
//[a-z&&[^bc]]  表示a-z和非bc的交集   相当于[ad-z]
        System.out.println("a".matches("[a-z&&[^bc]]"));//true
        System.out.println("b".matches("[a-z&&[^bc]]"));//false
        System.out.println("0".matches("[a-z&&[^bc]]"));//false
//      .表示任意一个字符
        System.out.println("a".matches("."));//true
        System.out.println("a".matches(".."));//false
        System.out.println("aa".matches(".."));//true
        //    \\d表示任意的一个数字
        System.out.println("3".matches("\\d"));//true
        System.out.println("a".matches("\\d"));//false
        System.out.println("33".matches("\\d"));//false
        System.out.println("33".matches("\\d\\d"));//true
        //  \\w只能是一位字符 :[A-Za-z_0-9]
        System.out.println("a".matches("\\w"));//true
        System.out.println("3".matches("\\w"));//true
        System.out.println("33".matches("\\w"));//false
        System.out.println("_".matches("\\w"));//true
        //   \\W表示一个非单词字符,相当于\\w取反
        System.out.println("你".matches("\\W"));//true
        //以上正则表达式只能校验单个字符
 
- 判断多个字符:
 
 
- 例子:
//必须是数字,字母,下划线,至少6位
System.out.println("11wwdwdwdwa".matches("\\w{6,}"));//true
System.out.println("wer".matches("\\w{6,}"));//false
//必须是数字和字符,必须是4位
System.out.println("223df".matches("[a-zA-Z_]{4}"));//false
System.out.println("wd23_".matches("[\\w&&[^_]]{4}"));//false
System.out.println("wd23_www".matches("\\w&&[^_]{4}"));//false
 
- 忽略大小写的书写方式:(?i)
//忽略大小写的书写方式:(?i)
        //在匹配时忽略abc的书写方式
        String  regex1= "(?i)abc";
        System.out.println("ABC".matches(regex1));//true
        System.out.println("abc".matches(regex1));//true
        System.out.println("Abc".matches(regex1));//true
        //在匹配时忽略bc的书写方式
        String  regex2= "a(?i)bc";
        System.out.println("ABC".matches(regex2));//false
        System.out.println("abc".matches(regex2));//true
        System.out.println("Abc".matches(regex2));//false