1、基本的字符匹配

 1 package regex;
 2 
 3 public class TestReg01 {
 4     public static void main(String[] args) {
 5         //"."表示任意字符
 6         System.out.println("a".matches("."));
 7         System.out.println("as".matches(".s"));
 8         // \d表示是否为数字
 9         System.out.println("123".matches("\d\d\d"));
10         // \D表示是否不是数字,大写与小写相反。
11         System.out.println("rtz2ez".matches("\D\D\D\d\D\D"));
12         // \s表示是为空白字符
13         System.out.println("1  2  1".matches("\d\s\s\d\s\s\d"));
14         // tab也算空白字符
15         System.out.println("1        2    1".matches("\d\s\s\d\s\d"));
16         // \w表示常用字符:a-z,A-Z,_,0-9
17         System.out.println("aa b1 22".matches("\w\w\s\w\d\s\d\d"));
18         // [abcd]表示要匹配的这个字符是否为abcd中的某一个字符
19         System.out.println("a".matches("[abcd]"));
20         // [a-zA-D]表示是否为在a-z,A-D之间的字符
21         System.out.print("b".matches("[a-zA-D]")+",");
22         System.out.println("b".matches("[a-z[A-D]]"));
23         // [a-zA-D]表示是否为不在a-z,A-D之间的字符
24         System.out.println("X".matches("[^a-zA-D]"));
25         // [a-z&&[def]]表示在a-z中间并且是def中的一个
26         System.out.println("f".matches("[a-z&&[def]]"));
27         // [a-z&&[def]]表示在a-z中间并且是def中的一个
28         System.out.println("a".matches("[a-z||[def]]"));
29         System.out.println("b".matches("[a-z&&[^bc]]"));
30                 
31 //        [a-d[m-p]]    a through d, or m through p: [a-dm-p] (union)并集
32 //        [a-z&&[def]]    d, e, or f (intersection) 交集
33 //        [a-z&&[^bc]]    a through z, except for b and c: [ad-z] (subtraction)
34 //        [a-z&&[^m-p]]    a through z, and not m through p: [a-lq-z](subtraction)
35     }
36 }
原文地址:https://www.cnblogs.com/Akke/p/5003117.html