正则表达式(regularexpression)描述了一种字符串匹配模式(pattern),可以用来检查一个字符串是否包含某个子串,替换匹配的从一个字符串中提取满足某个条件的子串字符串等常用正则表达式常用字符普通字符普通字符主要讲解以下内容,并举例说明//StringregStr="[a-z]";//匹配a-z中的任意字符//StringregStr="[A-Z]";//匹配A-Z中任意字符//StringregStr="abc";//匹配字符串abc//StringregStr="(?i)abc";//匹配字母abc不区分大小写//StringregStr="[0-9]";//匹配0-9的任意字符//StringregStr="[^0-9]";//匹配0-9以外的任意字符//StringregStr="[^0-9]{2}";//匹配2个非0-9的字符//StringregStr="\\d";//匹配任意数字字符,相当于[0-9]//StringregStr="\\D";//匹配任意非数字字符,相当于[^0-9]//StringregStr="\\w";//匹配任意数字,字母,下划线,相当于[0-9a-zA-Z_]//StringregStr="\\W";//匹配除数字、字母、下划线以外的任意一个,相当于[^0-9a-zA-Z_]//StringregStr="\\s";//匹配任意一个空字符//StringregStr="\\S";//匹配任意非空字符//StringregStr="ab|cd";//选择匹配字符,匹配字符串ab或cd1)StringregStr="[a-z]";//匹配a-z中任意字符@Testpublicvoidtest1(){Stringstr="abc2021";StringregStr="[a-z]";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据是:"+matcher.group(0));}}结果展示2)StringregStr="[A-Z]";//匹配A-Z任意字符@Testpublicvoidtest2(){Stringstr="ABCabc2021";StringregStr="[A-Z]";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果展示3)StringregStr="abc";//匹配字符串abc@Testpublicvoidtest2(){Stringstr="ABCabc2021";StringregStr="abc";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果展示4)StringregStr="(?i)abc";//匹配字母abc,不区分大小写@Testpublicvoidtest2(){Stringstr="ABCabc2021";StringregStr="(?i)abc";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果显示5)StringregStr="[0-9]";//匹配0-9任意字符@Testpublicvoidtest2(){Stringstr="ABCabc2021";StringregStr="[0-9]";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果显示6)StringregStr="[^0-9]";//匹配的不是0-9中的任何字符@Testpublicvoidtest2(){Stringstr="ABCabc2021";StringregStr="[^0-9]";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果显示限定符/***限定符**:表示出现任意次数,0次或n次,如(abc)*表示abc出现0次或多次**+:表示至少出现1次或n次,如(abc)+表示abc出现1次或多次*?:表示至少出现0次或1次,例如abc?表示c出现0次或1次*{n}:表示出现n次,如[0-9]{2},表示匹配2次。数字*{n,}表示至少匹配n次,如[0-9]{3,}表示匹配至少3次数字*{n,m}表示至少n次,最多m次,如as[0-9]{2,4}表示匹配次数为数字的2-4次*/1)*:表示出现任意次数,0次或n次@Testpublicvoidtest2(){Stringstr="zypabcabc2021";StringregStr="zyp(abc)*";模式通信pile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果显示2)+:表示至少出现1次或n次,如(abc)+表示abc出现1次或多次@Testpublicvoidtest2(){Stringstr="zypabc2021";StringregStr="zyp(abc)+";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果显示3)?:表示至少出现0次或1次,例如abc?表示c出现0次或1次@Testpublicvoidtest2(){Stringstr="zyp2021";StringregStr="zyp(abc)?";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果展示4){n}:表示n次,如[0-9]{2},表示匹配2个数字@Testpublicvoidtest2(){Stringstr="zyp2021";StringregStr="[0-9]{2}";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println(&quo吨;匹配到的数据为:"+matcher.group(0));}}结果显示5){n,}表示至少出现n次,如[0-9]{3,}表示匹配至少3个数字@Testpublicvoidtest2(){Stringstr="zyp2021";StringregStr="[0-9]{2,}";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果展示6){n,m}表示最少n次,最多m次,如[0-9]{2,4}表示2-4次匹配的次数@Testpublicvoidtest2(){Stringstr="zyp2021";StringregStr="[0-9]{2,4}";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果展示locator/***Locator*^:表示字符串开头的意思,例如:有一个字符串123abc,正则为^[0-9]+[a-z]*(必须以数字开头),则可以匹配成功.如果字符如果字符串isa123abc,它不会匹配*$:表示字符串以什么结尾。例如:如果有字符串123abc,正则表达式为^[0-9]+[a-z]+$(表示以数字开头,以字母结尾)则可以匹配成功。如果字符串为a123abc1,则无法匹配*\\b:表示边匹配(字符串结尾或空格后)。有一个字符串abc123abc,正则为abc\\b,匹配到的是最后一个abc*\\B:与\\b*/1)相反^:表示以什么开头的字符串@Testpublicvoidtest2(){Stringstr="2021zyp";StringregStr="^[0-9]+";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据是:"+matcher.group(0));}}结果显示2)$:表示字符串结束的地方@Testpublicvoidtest2(){Stringstr="2021zyp";StringregStr="[0-9]$";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}没有匹配,因为它以数字3)\\b结尾:表示边匹配(在字符串末尾或空格后)@Testpublicvoidtest2(){Stringstr="zyp2021zyp";StringregStr="zyp\\b";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}匹配最后一个“zyp”4)\\B:与\\b相反@Testpublicvoidtest2(){Stringstr="zyp2021zyp";StringregStr="zyp\\B";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}匹配第一个“zyp”组/***组:可分为捕获组和非捕获组*1。捕获组:*1)如(\\d\\d)(\\d\\d)表示匹配4位,如果字符串是2021abcd,*我们通过matcher.group(0)得到2021*得到20*通过matcher.group(1)。group(2)得到21*可见()起到了分组的作用**2)比如(?
