-
进行对数据的验证
[] //单个字符 [1-9] //1~9之间的数据 [ABC] //AB或者C [^ABCD] //^排除 [A-Za-z] [a-f[t-p]] //并集 a-f或者t-p的并集 [a-z && [QWE]] //交集 [A-Z && [^IOP]] //减去IOP [A-Z && [^T-W]] //减去T-W \d //0-9的数字 . //任何字符 \D //非数字的情况,对于[^0-9] \s //空白字符 \S //非空字符 \w //单词字符 \W //非单词字符 x? //x,一次或者一次都没有 x* //零次或者多次 x+ //一次或者多次 x{n} //N次 x{N,M} //N次但是不能超过M次
-
例子1:
public static void main(String[] agrs){ String str = "[1-9]\\d{4-11}"; System.out.println("1234564".matches(str)); }
-
例子2:
public static void main(String[] args){ String s = "我的手机号是:19122542846"; String str = "1[35789]\\d{9}"; Pattern compile = Patten.compile(str); Matcher matcher = compile.matcher(s); while(matcher.find()){ System.out.println("matcher.group() = " + matcher.group()); } }