正则表达式-字符类
1. \[abc\]:代表a或者b,或者c字符中的一个。
2. \[^abc\]:代表除a,b,c以外的任何字符。
3. [a-z]:代表a-z的所有小写字符中的一个。
4. [A-Z]:代表A-Z的所有大写字符中的一个。
5. [0-9]:代表0-9之间的某一个数字字符。
6. [a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符。
7. [a-d[m-p]]:a 到 d 或 m 到 p之间的任意一个字符。
测试
package com.itheima.a08regexdemo;
public class RegexDemo2 {
public static void main(String[] args) {
System.out.println("-----------1-------------");
System.out.println("a".matches("[abc]"));
System.out.println("z".matches("[abc]"));
System.out.println("-----------2-------------");
System.out.println("a".matches("[^abc]"));
System.out.println("z".matches("[^abc]"));
System.out.println("zz".matches("[^abc]"));
System.out.println("zz".matches("[^abc][^abc]"));
System.out.println("-----------3-------------");
System.out.println("a".matches("[a-zA-z]"));
System.out.println("z".matches("[a-zA-z]"));
System.out.println("aa".matches("[a-zA-z]"));
System.out.println("zz".matches("[a-zA-Z]"));
System.out.println("zz".matches("[a-zA-Z][a-zA-Z]"));
System.out.println("0".matches("[a-zA-Z]"));
System.out.println("0".matches("[a-zA-Z0-9]"));
System.out.println("-----------4-------------");
System.out.println("a".matches("[a-d[m-p]]"));
System.out.println("d".matches("[a-d[m-p]]"));
System.out.println("m".matches("[a-d[m-p]]"));
System.out.println("p".matches("[a-d[m-p]]"));
System.out.println("e".matches("[a-d[m-p]]"));
System.out.println("0".matches("[a-d[m-p]]"));
System.out.println("----------5------------");
System.out.println("a".matches("[a-z&[def]]"));
System.out.println("d".matches("[a-z&&[def]]"));
System.out.println("0".matches("[a-z&&[def]]"));
System.out.println("-----------6------------_");
System.out.println("a".matches("[a-z&&[^bc]]"));
System.out.println("b".matches("[a-z&&[^bc]]"));
System.out.println("0".matches("[a-z&&[^bc]]"));
System.out.println("-----------7-------------");
System.out.println("a".matches("[a-z&&[^m-p]]"));
System.out.println("m".matches("[a-z&&[^m-p]]"));
System.out.println("0".matches("[a-z&&[^m-p]]"));

正则表达式-预定义字符
3. "\D":任何非数字\[^0-9\]的简写;
4. "\\s": 空白字符:[ \t\n\x0B\f\r] 的简写
5. "\S": 非空白字符:\[^\s\] 的简写
6. "\\w":单词字符:[a-zA-Z_0-9]的简写
public static void main(String[] args) {
System.out.println("你".matches(".."));
System.out.println("你".matches("."));
System.out.println("你a".matches(".."));
System.out.println("a".matches("\\d"));
System.out.println("3".matches("\\d"));
System.out.println("333".matches("\\d"));
System.out.println("z".matches("\\w"));
System.out.println("2".matches("\\w"));
System.out.println("21".matches("\\w"));
System.out.println("你".matches("\\w"));
System.out.println("你".matches("\\W"));
System.out.println("---------------------------------------------");
System.out.println("2442fsfsf".matches("\\w{6,}"));
System.out.println("244f".matches("\\w{6,}"));
System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));
System.out.println("23 F".matches("[a-zA-Z0-9]{4}"));
System.out.println("23dF".matches("[\\w&&[^_]]{4}"));
System.out.println("23_F".matches("[\\w&&[^_]]{4}"));
正则表达式-数量词
6. X{n,m}: n到m次(n和m都是包含的)
public static void main(String[] args) {
System.out.println("2442fsfsf".matches("\\w{6,}"));
System.out.println("244f".matches("\\w{6,}"));
System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));
System.out.println("23 F".matches("[a-zA-Z0-9]{4}"));
System.out.println("23dF".matches("[\\w&&[^_]]{4}"));
System.out.println("23_F".matches("[\\w&&[^_]]{4}"));