当前位置: 首页 > 科技观察

LinuxShell正则表达式介绍

时间:2023-03-21 15:58:35 科技观察

正则表达式(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)比如(?\\d\\d)(?\\d\\d)的意思匹配4位数字,如果字符串是2021abcd,*我们通过matcher.group(0)得到2021*通过matcher.group(1)得到20,通过matcher.group(a1)*得到20*通过matcher.group(2)得到21,也可以通过matcher.group(a2)得到21*,这说明()起到了分组**2的作用。非捕获分组:无法通过group(1)或group(2)*1)获取值如20(?:20|21|22)表示匹配2020|2021|2022*2)如20(?:20|21|22)=20|21|22)表示匹配2020或2021或2022中的20*3)如20(?!20|21|22)表示不匹配2020或2021或2022中的20,匹配其他20**/capturegroup1)suchas(\\d\\d)(\\d\\d)表示匹配4位数字,如果字符串是2021abcd,@Testpublicvoidtest2(){Stringstr="2021abcd";StringregStr="(\\d\\d)(\\d\\d)";Patterncompile=Pattern.compile(regStr);Matchermatcher=编译.matcher(str);while(matcher.find()){System.out.println("matcher.group(0):"+matcher.group(0));System.out.println("第一组:matcher.group(1):"+matcher.group(1));System.out.println("Group2:matcher.group(2):"+matcher.group(2));}}结果展示结论:可以看出()将正则表达式分组,并依次给出数字,从12)(?\\d\\d)(?\\d\\d)开始表示匹配4位数字,如果字符String2021abcd@Testpublicvoidtest2(){Stringstr="2021abcd";StringregStr="(?\\d\\d)(?\\d\\d)";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("matcher.group(0):"+matcher.group(0));System.out.println("第1组:matcher.group(1):"+matcher.group(1));System.out.println("第2组:matcher.group(2):"+matcher.group(2));System.out.println("组名a1:matcher.group(1):"+matcher.group("a1"));System.out.println("组名a2:matcher.group(2):"+matcher.group("a2"));}}结果展示结论:可以看出()不仅可以对正则表达式进行分组,还可以依次给定编号,从1开始也可以给分组起个名字,根据名字获取对应的匹配值。非捕获组1)比如20(?:20|21|22)表示匹配2020|2021|2022@Testpublicvoidtest2(){Stringstr="2021a2022B2023";StringregStr="20(?:20|21|22)";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据是:"+matcher.group(0));}}结果展示2)比如20(?=20|21|22)表示匹配20年或者2021年或者2022@Testpublicvoidtest2(){Stringstr="2021a2022B2023";StringregStr="20(?=20|21|22)";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}这里匹配的20是2021和2022中的203)比如20(?!20|21|22)表示匹配不匹配2020或者2021或者2022中的20,匹配其他的20@Testpublicvoidtest2(){Stringstr="2021a2022B2023";StringregStr="20(?!20|21|22)";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据是:"+matcher.group(0));}}这里匹配到的20是2023/***backreferences*中的20个backreferences如果我们要在一个字符串中查找4个连续的数字,并且第一个和第四个数字必须相同,第二个和第三个数字必须相同*this当我们使用backreferences时,很简单*反向引用的内部用法:\\n其中n代表组号,如:字符串12345678870008,正则为(\\d)(\\d)\\2\\1*外部用法ofbackreference:$n其中n代表组号*/string12345678870008,正则为(\\d)(\\d)\\2\\1@Testpublicvoidtest2(){Stringstr="12345678870008";/***第一个(\\d)会被赋值一组1*第二个(\\d)会被赋值一组2*\\2:表示引用第2组的值,所以2和3的值会一样*\\1:表示引用组1的值,所以1和4的值会一样*/StringregStr="(\\d)(\\d)\\2\\1";Patterncompile=Pattern.compile(regStr);Matchermatcher=compile.matcher(str);while(matcher.find()){System.out.println("匹配到的数据为:"+matcher.group(0));}}结果展示