Java—— 正则表达式
作用
1.校验字符串是否满足规则
2.在一段文本中查找满足要求的内容
字符类(只匹配一个字符)
[abc] | 只能是a,b,或c |
[^abc] | 除了a,b,c之外的任何字符 |
[a-zA-Z] | a到z A到Z,包括(范围) |
[a-d[m-p]] | a到d,或m到p |
[a-z&&[def]] | a-z和def的交集,为:d,e,f |
[a-z&&[^bc]] | a-z和非bc的交集,(等同于[ad-z]) |
[a-z&&[^m-p]] | a到z和除了m到p的交集,(等同于[a-lq-z]) |
代码演示:
public class RegexDemo2 {public static void main(String[] args) {//public boolean matches(String regex):判断是否与正则表达式匹配,匹配返回true// 只能是a b cSystem.out.println("-----------1-------------");System.out.println("a".matches("[abc]")); // trueSystem.out.println("z".matches("[abc]")); // false// 不能出现a b cSystem.out.println("-----------2-------------");System.out.println("a".matches("[^abc]")); // falseSystem.out.println("z".matches("[^abc]")); // trueSystem.out.println("zz".matches("[^abc]")); //falseSystem.out.println("zz".matches("[^abc][^abc]")); //true// a到zA到Z(包括头尾的范围)System.out.println("-----------3-------------");System.out.println("a".matches("[a-zA-z]")); // trueSystem.out.println("z".matches("[a-zA-z]")); // trueSystem.out.println("aa".matches("[a-zA-z]"));//falseSystem.out.println("zz".matches("[a-zA-Z]")); //falseSystem.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); //trueSystem.out.println("0".matches("[a-zA-Z]"));//falseSystem.out.println("0".matches("[a-zA-Z0-9]"));//true// [a-d[m-p]] a到d,或m到pSystem.out.println("-----------4-------------");System.out.println("a".matches("[a-d[m-p]]"));//trueSystem.out.println("d".matches("[a-d[m-p]]")); //trueSystem.out.println("m".matches("[a-d[m-p]]")); //trueSystem.out.println("p".matches("[a-d[m-p]]")); //trueSystem.out.println("e".matches("[a-d[m-p]]")); //falseSystem.out.println("0".matches("[a-d[m-p]]")); //false// [a-z&&[def]] a-z和def的交集。为:d,e,fSystem.out.println("----------5------------");System.out.println("a".matches("[a-z&[def]]")); //falseSystem.out.println("d".matches("[a-z&&[def]]")); //trueSystem.out.println("0".matches("[a-z&&[def]]")); //false// [a-z&&[^bc]] a-z和非bc的交集。(等同于[ad-z])System.out.println("-----------6------------_");System.out.println("a".matches("[a-z&&[^bc]]"));//trueSystem.out.println("b".matches("[a-z&&[^bc]]")); //falseSystem.out.println("0".matches("[a-z&&[^bc]]")); //false// [a-z&&[^m-p]] a到z和除了m到p的交集。(等同于[a-1q-z])System.out.println("-----------7-------------");System.out.println("a".matches("[a-z&&[^m-p]]")); //trueSystem.out.println("m".matches("[a-z&&[^m-p]]")); //falseSystem.out.println("0".matches("[a-z&&[^m-p]]")); //false}
}
预定义字符(只匹配一个字符)
. | 任何字符 |
\d | 一个数字:[0-9] |
\D | 非数字:[^0-9] |
\s | 一个空白字符:[\t\n\x0B\f\r] |
\S | 非空白字符:[^\s] |
\w | [a-zA-Z_0-9] 英文、数字、下划线 |
\W | [^\w] 一个非单词字符 |
代码演示:
public class RegexDemo3 {public static void main(String[] args) {// \ 转义字符 改变后面那个字符原本的含义//练习:以字符串的形式打印一个双引号//"在Java中表示字符串的开头或者结尾//此时\表示转义字符,改变了后面那个双引号原本的含义//把他变成了一个普普通通的双引号而已。System.out.println("\"");// \表示转义字符//两个\的理解方式:前面的\是一个转义字符,改变了后面\原本的含义,把他变成一个普普通通的\而已。System.out.println("c:Users\\moon\\IdeaProjects\\basic-code\\myapi\\src\\com\\itheima\\a08regexdemo\\RegexDemo1.java");//.表示任意一个字符System.out.println("你".matches("..")); //falseSystem.out.println("你".matches(".")); //trueSystem.out.println("你a".matches(".."));//true// \\d 表示任意的一个数字// \\d只能是任意的一位数字// 简单来记:两个\表示一个\System.out.println("a".matches("\\d")); // falseSystem.out.println("3".matches("\\d")); // trueSystem.out.println("333".matches("\\d")); // false//\\w只能是一位单词字符[a-zA-Z_0-9]System.out.println("z".matches("\\w")); // trueSystem.out.println("2".matches("\\w")); // trueSystem.out.println("21".matches("\\w")); // falseSystem.out.println("你".matches("\\w"));//false// 非单词字符System.out.println("你".matches("\\W")); // true}
}
数量词
x? | X,0次或1次 |
X* | X,0次或多次 |
X+ | X,1次或多次 |
x{n} | X,正好n次 |
x{n,} | X,至少n次 |
x{n,m} | X,至少n但不超过m次 |
代码演示:
public class Test5 {public static void main(String[] args) {//必须是数字 字母 下划线 0次或1次System.out.println("".matches("\\w?"));//trueSystem.out.println("a".matches("\\w?"));//trueSystem.out.println("aa".matches("\\w?"));//false//必须是数字 字母 下划线 0次或多次System.out.println("".matches("\\w*"));//trueSystem.out.println("a".matches("\\w*"));//trueSystem.out.println("aa".matches("\\w*"));//true//必须是数字 字母 下划线 1次或多次System.out.println("".matches("\\w+"));//falseSystem.out.println("a".matches("\\w+"));//trueSystem.out.println("aa".matches("\\w+"));//true// 必须是数字 字母 下划线 至少 6位System.out.println("2442fsfsf".matches("\\w{6,}"));//trueSystem.out.println("244f".matches("\\w{6,}"));//false// 必须是数字和字符 必须是4位System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));//trueSystem.out.println("23 F".matches("[a-zA-Z0-9]{4}"));//falseSystem.out.println("23dF".matches("[\\w&&[^_]]{4}"));//trueSystem.out.println("23_F".matches("[\\w&&[^_]]{4}"));//false// 必须是数字 字母 下划线 至少3但不超过6次System.out.println("".matches("\\w{3,6}"));//falseSystem.out.println("12".matches("\\w{3,6}"));//falseSystem.out.println("123".matches("\\w{3,6}"));//trueSystem.out.println("123456".matches("\\w{3,6}"));//trueSystem.out.println("1234567".matches("\\w{3,6}"));//false}
}