Java_正则表达式(15)

字符类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Test {
  public static void main(String[] args) {
    //[abc]、[ ^abc ]
    String regex1 = "[abc]"; //[]代表单个字符,这里即匹配a或b或c
    System.out.println("ab".matches(regex1)); //false
    String regex2 = "[^abc]"; //除了abc其他任意单个字符
    System.out.println("d".matches(regex2)); //true
    System.out.println("1".matches(regex2)); //true
    System.out.println("10".matches(regex2)); //false

    //[a-zA-Z]、[a-z[A-Z]]
    String regex3 = "[a-zA-Z]"; //a到z或A到Z,包含头包含尾
    System.out.println("a".matches(regex3)); //true
    System.out.println("A".matches(regex3)); //true
    System.out.println("z".matches(regex3)); //true
    System.out.println("Z".matches(regex3)); //true
    System.out.println("1".matches(regex3)); //false

    //[a-z&&[def]]
    String regex4 = "[a-z&&[def]]"; //&&取交集,范围是def
    System.out.println("a".matches(regex4)); //false
    System.out.println("d".matches(regex4)); //true

    //[a-z&&[ ^m-p]]
    String regex5 = "[a-z&&[^m-p]]"; //a到z,除了m到p取交集,即[a-lq-z]
    System.out.println("a".matches(regex5)); //true
    System.out.println("n".matches(regex5)); //false
    System.out.println("z".matches(regex5)); //true
  }
}

预定义字符

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Test {
  public static void main(String[] args) {
    //. and ..
    String regex1 = ".";  //一个任意字符
    String regex2 = ".."; //两个任意字符
    System.out.println("a".matches(regex1)); //true
    System.out.println("aa".matches(regex2)); //true

    //\d 、 \D
    String regex3 = "\\d"; //[0-9]
    String regex4 = "\\D"; //[^0-9]
    System.out.println("1".matches(regex3)); //true
    System.out.println("1".matches(regex4)); //false

    //\s 、 \S
    String regex5 = "\\s"; //空白字符[ \t\n\x0B\f\r],[]中第一个是空格
    String regex6 = "\\S"; //非空白字符[^\s]
    System.out.println(" ".matches(regex5));  //true
    System.out.println(" ".matches(regex6));  //false

    //\w 、 \W
    String regex7 = "\\w"; //单词字符[a-zA-Z_0-9]
    String regex8 = "\\W"; //非单词字符[^\w]
    System.out.println("a".matches(regex7)); //true
    System.out.println(";".matches(regex8)); //true
  }
}

数量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Test {
  public static void main(String[] args) {
    //?
    String regex1 = "[abc]?"; //?表示0次或1次
    System.out.println("".matches(regex1)); //true
    System.out.println("a".matches(regex1)); //true
    //[abc]?的?是针对abc,a或b或c可以出现1次或0次,但不能出现其他字符
    System.out.println("d".matches(regex1)); //false

    //*
    String regex2 = "[abc]*"; //*表示0次或多次,1次也包括
    System.out.println("".matches(regex2)); //true
    System.out.println("a".matches(regex2)); //true
    System.out.println("aa".matches(regex2)); //true
    System.out.println("aabcc".matches(regex2)); //true
    System.out.println("d".matches(regex2)); //false

    //+
    String regex3 = "[abc]+"; //+表示1次或多次
    System.out.println("".matches(regex3)); //false
    System.out.println("a".matches(regex3)); //true
    System.out.println("aa".matches(regex3)); //true
    System.out.println("aabcc".matches(regex3)); //true
    System.out.println("d".matches(regex3)); //false

    //{n}
    String regex4 = "[abc]{5}"; //{5}表示恰好有5次
    System.out.println("abcba".matches(regex4)); //true
    System.out.println("abcb".matches(regex4)); //false
    System.out.println("abcbaa".matches(regex4)); //false

    //{n,}
    String regex5 = "[abc]{5,}"; //{5,}表示至少5次,包括5
    System.out.println("abcba".matches(regex5)); //true
    System.out.println("abcbaabbc".matches(regex5)); //true
    System.out.println("abcb".matches(regex5)); //false

    //{n,m}
    String regex6 = "[abc]{5,10}"; //{5,10}表示5到10次
    System.out.println("abcba".matches(regex6)); //true
    System.out.println("abcbaabbcc".matches(regex6)); //true
    System.out.println("abcbabcbcaaac".matches(regex6)); //false
    System.out.println("abcb".matches(regex6)); //false
  }
}

split

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Test {
  public static void main(String[] args) {
    String s1 = "hello world";
    String[] arr1 = s1.split(" ");
    for (int i = 0; i < arr1.length; i++)
      System.out.print(arr1[i] + " ");
    //hello world

    String s2 = "hello.world";
    //.表示匹配任意字符,所以要转义
    String[] arr2 = s2.split("\\."); 
    for (int i = 0; i < arr2.length; i++)
      System.out.println(arr2[i]);
    //hello world
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Test {
  public static void main(String[] args) {
    String s = "91 27 46 38 50";
    //切割字符串
    String[] sArr = s.split(" ");
    int[] arr = new int[sArr.length];
    for (int i = 0; i < arr.length; i++)
      arr[i] = Integer.parseInt(sArr[i]);
    Arrays.sort(arr);
//		String str = "";
//		for (int i = 0; i < arr.length; i++) {
//			if (i == arr.length - 1) {
//				str += arr[i];
//			} else {
//				str += arr[i] + " ";
//			}
//		}
    //StringBuilder节省内存
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
      if (i == arr.length - 1) {
        sb.append(arr[i]);
      } else {
        sb.append(arr[i] + " ");
      }
    }
    System.out.println(sb); //27 38 46 50 91
  }
}

replaceAll

1
2
3
4
5
6
7
8
public class Test {
  public static void main(String[] args) {
    String s = "hello3world";
    String regex = "\\d"; //匹配任意数字
    String s2 = s.replaceAll(regex, " ");
    System.out.println(s2); //hello world
  }
}

分组 ()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Test {
  public static void main(String[] args) {
    //()表示分组
    //匹配 高高兴兴
    // \\1表示第1组再出现一次, \\2表示第2组再出现一次
    String regex1 = "(.)\\1(.)\\2";
    System.out.println("高高兴兴".matches(regex1)); //true
    System.out.println("高兴高兴".matches(regex1)); //false
    //匹配 高兴高兴
    String regex2 = "(..)\\1"; //让两个任意字符再出现一次
    System.out.println("高兴高兴".matches(regex2)); //true
    System.out.println("高高兴兴".matches(regex2)); //fasle

    //按照叠词切割
    String s = "sdqqfgkkkjppppkl";
    String regex = "(.)\\1+"; //让分组出现1次或者多次
    String[] arr = s.split(regex);
    for (int i = 0; i < arr.length; i++)
      System.out.print(arr[i] + " "); //sd fg j kl

    //将 我我....我...爱...爱爱..爱..Java 转换为我爱Java
    String s1 = "我我....我...爱...爱爱..爱..Java";
    String s2 = s1.replaceAll("\\.+", ""); //将出现一次或多次的.替换
    System.out.println(s2); //我我我爱爱爱爱Java
    String s3 = s2.replaceAll("(.)\\1+", "$1"); //$1表示第一个分组的值
    System.out.println(s3); //我爱Java
    String s4 = s2.replaceAll("(.)\\1+(.)\\2+", "$1$2"); //$2表示第二个分组的值
    System.out.println(s4); //我爱Java
  }
}

Pattern和Matcher

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Test {
  public static void main(String[] args) {
    Pattern p = Pattern.compile("a*b"); //获取正则表达式,a出现0到多次后面跟个b
    Matcher m = p.matcher("aaaaab"); //获取匹配器
    boolean b = m.matches(); //看匹配器是否与正则匹配,匹配返回true
    System.out.println(b); //true

    System.out.println("aaaaab".matches("a*b")); //效果同上
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class Test {
  public static void main(String[] args) {
    //把字符串中的手机号获取出来
    String regex = "1[3578]\\d{9}";
    String s = "我的手机号码是18912345678,之前是18987654321,还用过18812345678";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);
    //        //要先找
    //		System.out.println(m.find());  //true
    //        //再获取
    //		System.out.println(m.group()); //18912345678
    while (m.find()) {
      System.out.println(m.group());
    }
    //18912345678
    //18987654321
    //18812345678
  }
}

以上内容是玉山整理的笔记,如有错误还请指出