find()
:字符串某个部分匹配上正则表达式就会返回true
matches()
:整个字符串都匹配上正则表达式才返回true,否则false
参考:Java Matcher对象中find()与matches()的区别
参考:Pattern隐藏了哪些Java8追加的新功能
参考:Matcher类有哪些我们必须掌握的方法?
Pattern compile = Pattern.compile("[WASD][0-9]{1,2}");
System.out.println(compile.matcher("A1").find());
System.out.println(compile.matcher("A12").find());
System.out.println(compile.matcher("E12").find());
System.out.println(compile.matcher("AE12").find());
System.out.println(compile.matcher("AEA12").find());
System.out.println(compile.matcher("AEA1A2").find());
System.out.println(compile.matcher("AEA12444").find());
Pattern compile = Pattern.compile("[WASD][0-9]{1,2}");
System.out.println(compile.matcher("A122").matches());
System.out.println(compile.matcher("AA12").matches());
System.out.println(compile.matcher("A12").matches());
System.out.println(compile.matcher("A1").matches());
System.out.println(compile.matcher("AA1").matches());
System.out.println(compile.matcher("AA12").matches());
System.out.println("A122".matches("[WASD][0-9]{1,2}"));
System.out.println("AA12".matches("[WASD][0-9]{1,2}"));
System.out.println("A12".matches("[WASD][0-9]{1,2}"));
System.out.println("A1".matches("[WASD][0-9]{1,2}"));
System.out.println("A1".matches("[WASD]{1,2}[0-9]{1,2}"));
System.out.println("A12".matches("[WASD]{1,2}[0-9]{1,2}"));
System.out.println("AA1".matches("[WASD]{1,2}[0-9]{1,2}"));
System.out.println("AA12".matches("[WASD]{1,2}[0-9]{1,2}"));
- 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