字符类:
[abc] a、b或c(简单类)
String regex = "[a,b,c]"; //[]代表单个字符
System.out.println("a".matches(regex)); //true
System.out.println("b".matches(regex)); //true
System.out.println("c".matches(regex)); //true
System.out.println("d".matches(regex)); //false
System.out.println("1".matches(regex)); //false
System.out.println("%".matches(regex)); //false
[^abc] 任何字符,除了a、b或c(否定)
String regex = "[^abc]";
System.out.println("a".matches(regex)); //false
System.out.println("b".matches(regex)); //false
System.out.println("c".matches(regex)); //false
System.out.println("d".matches(regex)); //true
System.out.println("1".matches(regex)); //true
System.out.println("%".matches(regex)); //true
System.out.println("10".matches(regex)); //false,10代表字符1和字符0,是两个字
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母都包含在内(范围)
String regex = "[a-zA-Z]";
System.out.println("a".matches(regex)); //true
System.out.println("z".matches(regex)); //true
System.out.println("A".matches(regex)); //true
System.out.println("Z".matches(regex)); //true
System.out.println("1".matches(regex)); //false
System.out.println("%".matches(regex)); //false
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
String regex = "[a-d[m-p]]";
System.out.println("a".matches(regex)); //true
System.out.println("d".matches(regex)); //true
System.out.println("f".matches(regex)); //false
System.out.println("m".matches(regex)); //true
System.out.println("p".matches(regex)); //true
System.out.println("1".matches(regex)); //false
System.out.println("%".matches(regex)); //false
[a-z&&(def)] d、e或f,(交集)
String regex = "[a-z&&[def]]";
System.out.println("a".matches(regex)); //false
System.out.println("d".matches(regex)); //true
[a-z&&[^bc]] a 到 z,除了b和c:[ad-z](减去)
String regex = "[a-z&&[^bc]]";
System.out.println("a".matches(regex)); //true
System.out.println("b".matches(regex)); //false
System.out.println("c".matches(regex)); //false
System.out.println("z".matches(regex)); //true
System.out.println("1".matches(regex)); //false
System.out.println("%".matches(regex)); //false
[a-z&&[^m-p]] a 到 z,而非m 到 p:[a-lq-z](减去)
String regex = "[a-z&&[^m-p]]";
System.out.println("a".matches(regex)); //true
System.out.println("l".matches(regex)); //true
System.out.println("m".matches(regex)); //false
System.out.println("p".matches(regex)); //false
System.out.println("q".matches(regex)); //true
System.out.println("z".matches(regex)); //true
System.out.println("1".matches(regex)); //false
System.out.println("%".matches(regex)); //false
预定义字符类:
. 任意字符,有多少个点就代表多少个字符
String regex1 = "."; //一个.代表一个字符
String regex2 = ".."; //两个.代表两个字符
System.out.println("a".matches(regex1)); //true
System.out.println("ab".matches(regex1)); //false
System.out.println("a".matches(regex2)); //false
System.out.println("ab".matches(regex2)); //true
\d 单个数字:[0-9]
String regex = "\\d"; //\代表转义字符,如果需要\d就要把前面的\转义一次,所以就是\\d
System.out.println("0".matches(regex)); //true
System.out.println("9".matches(regex)); //true
System.out.println("a".matches(regex)); //false
System.out.println("%".matches(regex)); //false
\D 单个非数字:[^0-9]
String regex = "\\D";
System.out.println("0".matches(regex)); //false
System.out.println("9".matches(regex)); //false
System.out.println("a".matches(regex)); //true
System.out.println("%".matches(regex)); //true
\s 单个空白字符:[ \t\n\x0B\f\r]
String regex = "\\s";
System.out.println(" ".matches(regex)); //普通空格,true
System.out.println(" ".matches(regex)); //四个空格,false
System.out.println(" ".matches(regex)); //Tab键,true
System.out.println("\t".matches(regex)); //\t,true
System.out.println("\f".matches(regex)); //\f,true
System.out.println("\t".matches(regex)); //\t,true
System.out.println("\r".matches(regex)); //\r,true
\S 单个非空白字符:[^\s]
String regex = "\\S";
System.out.println(" ".matches(regex)); //false
System.out.println("1".matches(regex)); //true
System.out.println("a".matches(regex)); //true
System.out.println("%".matches(regex)); //true
\w 单个单词字符:[a-zA-Z_0-9]
String regex = "\\w";
System.out.println("0".matches(regex)); //true
System.out.println("a".matches(regex)); //true
System.out.println("%".matches(regex)); //false
System.out.println("_".matches(regex)); //true
\W 单个非单词字符:[^\w]
String regex = "\\W";
System.out.println("0".matches(regex)); //false
System.out.println("a".matches(regex)); //false
System.out.println("%".matches(regex)); //true
System.out.println("_".matches(regex)); //false
数量词:
X? X:任意字符,?:单个字符出现过一次或一次也没有,字符不存在不叫一次也没有
String regex = "[abc]?";
System.out.println("a".matches(regex)); //true
System.out.println("b".matches(regex)); //true
System.out.println("c".matches(regex)); //true
System.out.println("d".matches(regex)); //字符不存在,false
System.out.println("".matches(regex)); //一次也没有,true
X* X:任意字符,*:单个字符出现过零次或多次(包括一次,相当于零次到多次)
String regex = "[abc]*";
System.out.println("a".matches(regex)); //true
System.out.println("abc".matches(regex)); //true
System.out.println("".matches(regex)); //true
X+ X:任意字符,+:单个字符出现过一次或多次
String regex = "[abc]+";
System.out.println("a".matches(regex)); //true
System.out.println("aaabbbbcccc".matches(regex)); //true
System.out.println("".matches(regex)); //false
X{n} X:任意字符,{n}:每个字符出现次数的总和恰好出现n次,不能有其他字符
String regex = "[abc]{5}";
System.out.println("aaaaa".matches(regex)); //true
System.out.println("aaaas".matches(regex)); //false
System.out.println("11111".matches(regex)); //false
System.out.println("abcbas".matches(regex)); //false
System.out.println("abc".matches(regex)); //false
System.out.println("".matches(regex)); //false
X{n,} X:任意字符,{n,}:每个字符出现次数的总和至少出现n次,不能有其他字符
String regex = "[abc]{5,}";
System.out.println("aaaaa".matches(regex)); //true
System.out.println("111111".matches(regex)); //false
System.out.println("abcbaa".matches(regex)); //true
System.out.println("abcbas".matches(regex)); //false
System.out.println("abc".matches(regex)); //false
System.out.println("".matches(regex)); //false
X{n,m} X:任意字符,{n,m}:每个字符出现次数的总和至少出现n次,但不超过m次,不能有其他字符
String regex = "[abc]{5,7}";
System.out.println("aaaaa".matches(regex)); //true
System.out.println("111111".matches(regex)); //false
System.out.println("abcbaaa".matches(regex)); //true
System.out.println("abcbaaz".matches(regex)); //false
System.out.println("abcbaabc".matches(regex)); //false
System.out.println("abc".matches(regex)); //false
System.out.println("".matches(regex)); //false
正则表达式中的分割
split(String regex)
把正则表达式regex用做分隔符,并转换成字符串数组
String str1 = "hello,java,txt";
String str2 = "hello.java.txt";
System.out.println(Arrays.toString(str1.split(","))); //结果为[hello, java, txt]
System.out.println(Arrays.toString(str2.split("\\."))); //结果为[hello, java, txt]
正则表达式中的替换
replaceAll(String regex,String str)
regex:正则表达式
str:需要替换的字符串
String str1 = "a1b2c3d4e5";
System.out.println(str1.replaceAll("\\d", ""));//结果为abcde
正则表达式中的分组
// 叠词 快快乐乐,高高兴兴
String regex1 = "(.)\\1(.)\\2"; // (.)代表一组,\\1代表第一组又出现一次,\\2代表第二组又出现一次
System.out.println("快快乐乐".matches(regex1)); // true
System.out.println("快乐乐乐".matches(regex1)); // false
System.out.println("高高兴兴".matches(regex1)); // true
System.out.println("一二一二".matches(regex1)); // false
String regex2 = "(..)\\1";
System.out.println("快快乐乐".matches(regex2)); // false
System.out.println("快乐快乐".matches(regex2)); // true
System.out.println("高高兴兴".matches(regex2)); // false
System.out.println("一二一二".matches(regex2)); // true
String str = "abccdefffghiiiijk";
String regex = "(.)\\1+"; // +代表一组出现一次到多次
String[] strs = str.split(regex);
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i] + " "); // 结果为:ab de gh jk
}
String str = "我我....我...我要...要要...要学....学学..学.编..编编.编.程.程.程..程";
str = str.replaceAll("\\.+", "");
str = str.replaceAll("(.)\\1+", "$1"); // $1代表第一组中的内容
System.out.println(str); // 结果为:我要学编程
Pattern和Matcher的概述
compile(String regex)
获取正则表达式
matcher(CharSequence input)
获取匹配器
Pattern p = Pattern.compile("a*b"); // 获取正则表达式
Matcher m = p.matcher("aaaaab"); // 获取匹配器
boolean b = m.matches(); // 看是否能匹配
System.out.println(b); // 匹配则返回true
System.out.println("aaaaab".matches("a*b")); // 与上面结果一样的
正则表达式的获取功能
find()
查看在匹配器中是否满足正则表达式
group()
返回匹配器中的结果
String str = "我的第一个号码是15123456789,第二个号码是18123456789,第三个号码是13123456789";
Pattern p = Pattern.compile("1[3578]\\d{9}");
Matcher m = p.matcher(str);
while (m.find()) { //判断是否匹配
System.out.println(m.group()); //必须先要判断是否找到,然后才能打印,并且是循环依次打印
}