Linux Shell 正則表達式
簡介
正則表達式(regular expression)描述了一種字符串匹配的模式(pattern),可以用來檢查一個串是否含有某種子串、將匹配的子串替換或者從某個串中取出符合某個條件的子串等。
常用正則表達式
常用字符
普通字符
普通字符主要講解以下內(nèi)容,并舉例說明
- // String regStr = "[a-z]";//匹配a-z中任意一個字符
- // String regStr = "[A-Z]";//匹配A-Z中任何一個字符
- // String regStr = "abc";//匹配字符串a(chǎn)bc
- // String regStr = "(?i)abc";//匹配字母abc不區(qū)分大小寫
- // String regStr = "[0-9]";//匹配0-9任何一個字符
- // String regStr = "[^0-9]";//匹配不是0-9中的任何一個字符
- // String regStr = "[^0-9]{2}";//匹配2個不是0-9的字符
- // String regStr = "\\d";//匹配任何一個數(shù)字字符,等價于[0-9]
- // String regStr = "\\D";//匹配任何一個非數(shù)字字符,等價于[^0-9]
- // String regStr = "\\w";//匹配任何一個數(shù)字、字母、下劃線,等價于[0-9a-zA-Z_]
- // String regStr = "\\W";//匹配任何一個除了數(shù)字、字母、下劃線,等價于[^0-9a-zA-Z_]
- // String regStr = "\\s";//匹配任何一個空字符
- // String regStr = "\\S";//匹配任何一個非空字符
- // String regStr = "ab|cd";//選擇匹配符,匹配字符串a(chǎn)b或者cd
1) String regStr = "[a-z]";//匹配a-z中任意一個字符
- @Test
- public void test1() {
- String str = "abc2021";
- String regStr = "[a-z]";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
2) String regStr = "[A-Z]";//匹配A-Z中任何一個字符
- @Test
- public void test2(){
- String str = "ABCabc2021";
- String regStr = "[A-Z]";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
3)String regStr = "abc";//匹配字符串a(chǎn)bc
- @Test
- public void test2(){
- String str = "ABCabc2021";
- String regStr = "abc";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
4)String regStr = "(?i)abc";//匹配字母abc不區(qū)分大小寫
- @Test
- public void test2(){
- String str = "ABCabc2021";
- String regStr = "(?i)abc";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
5) String regStr = "[0-9]";//匹配0-9任何一個字符
- @Test
- public void test2(){
- String str = "ABCabc2021";
- String regStr = "[0-9]";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
6) String regStr = "[^0-9]";//匹配不是0-9中的任何一個字符
- @Test
- public void test2(){
- String str = "ABCabc2021";
- String regStr = "[^0-9]";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
限定符
- /**
- * 限定符
- * *:表示出現(xiàn)任意次數(shù),0次或者n次,如(abc)*表示abc出現(xiàn)0次或者多次
- * +:表示出現(xiàn)至少1次或者n次,如(abc)+表示abc出現(xiàn)1次或者多次
- * ?:表示出現(xiàn)至少0次或者1次,如abc?表示c出現(xiàn)0次或者1次
- * {n}:表示出現(xiàn)n次,如[0-9]{2},表示匹配2次數(shù)字
- * {n,}表示至少出現(xiàn)n次,如[0-9]{3,}表示匹配至少3次數(shù)字
- * {n,m}表示出現(xiàn)至少n次,最多m次,如[0-9]{2,4}表示匹配次數(shù)2-4次數(shù)字
- */
1) *:表示出現(xiàn)任意次數(shù),0次或者n次
- @Test
- public void test2(){
- String str = "zypabcabc2021";
- String regStr = "zyp(abc)*";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
2)+:表示出現(xiàn)至少1次或者n次,如(abc)+表示abc出現(xiàn)1次或者多次
- @Test
- public void test2(){
- String str = "zypabc2021";
- String regStr = "zyp(abc)+";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
3)?:表示出現(xiàn)至少0次或者1次,如abc?表示c出現(xiàn)0次或者1次
- @Test
- public void test2(){
- String str = "zyp2021";
- String regStr = "zyp(abc)?";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
4){n}:表示出現(xiàn)n次,如[0-9]{2},表示匹配2次數(shù)字
- @Test
- public void test2(){
- String str = "zyp2021";
- String regStr = "[0-9]{2}";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
5){n,}表示至少出現(xiàn)n次,如[0-9]{3,}表示匹配至少3次數(shù)字
- @Test
- public void test2(){
- String str = "zyp2021";
- String regStr = "[0-9]{2,}";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
6){n,m}表示出現(xiàn)至少n次,最多m次,如[0-9]{2,4}表示匹配次數(shù)2-4次數(shù)字
- @Test
- public void test2(){
- String str = "zyp2021";
- String regStr = "[0-9]{2,4}";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
定位符
- /**
- * 定位符
- * ^:表示字符串以什么開頭的意思。如:有一個字符串123abc,正則為^[0-9]+[a-z]*(必須已數(shù)字開頭),則能成功匹配上。如果字符串為a123abc則匹配不上
- * $:表示字符串以什么結(jié)束的意思。如:有一個字符串123abc,正則為^[0-9]+[a-z]+$(表示以數(shù)字開頭,字母結(jié)尾)則能成功匹配上。如果字符串為a123abc1則匹配不上
- * \\b:表示邊緣匹配(字符串的結(jié)尾或者空格之后)。有一個字符串a(chǎn)bc123abc,正則為abc\\b,匹配到的為最后的那個abc
- * \\B:與\\b相反
- */
1) ^:表示字符串以什么開頭的意思
- @Test
- public void test2(){
- String str = "2021zyp";
- String regStr = "^[0-9]+";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
2) $:表示字符串以什么結(jié)束的意思
- @Test
- public void test2(){
- String str = "2021zyp";
- String regStr = "[0-9]$";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
沒有匹配到,因為要以數(shù)字結(jié)束
3) \\b:表示邊緣匹配(字符串的結(jié)尾或者空格之后)
- @Test
- public void test2(){
- String str = "zyp2021zyp";
- String regStr = "zyp\\b";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
匹配到的是最后一個“zyp”
4) \\B:與\\b相反
- @Test
- public void test2(){
- String str = "zyp2021zyp";
- String regStr = "zyp\\B";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
匹配到的是第一個“zyp”
分組
- /**
- * 分組:可分為捕獲分組和非捕獲分組
- * 1.捕獲分組:
- * 1)如(\\d\\d)(\\d\\d)表示匹配4位數(shù)字,如果字符串位2021abcd,
- * 我們通過matcher.group(0)得到2021
- * 通過matcher.group(1)得到20
- * 通過matcher.group(2)得到21
- * 由此可見()起到分組的作用
- *
- * 2)如(?<a1>\\d\\d)(?<a2>\\d\\d)表示匹配4位數(shù)字,如果字符串位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)表示匹配2020或2021或2022中的20
- * 3)如20(?!20|21|22)表示匹配不匹配2020或2021或2022中的20,匹配其它20
- *
- */
捕獲分組
1)如(\\d\\d)(\\d\\d)表示匹配4位數(shù)字,如果字符串為2021abcd,
- @Test
- public void test2(){
- String str = "2021abcd";
- String regStr = "(\\d\\d)(\\d\\d)";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.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("分組二:matcher.group(2):"+matcher.group(2));
- }
- }
結(jié)果展示
結(jié)論:由此可見()會將正則分組,并按順序給出編號,從1開始
2) (?<a1>\\d\\d)(?<a2>\\d\\d)表示匹配4位數(shù)字,如果字符串位2021abcd
- @Test
- public void test2(){
- String str = "2021abcd";
- String regStr = "(?<a1>\\d\\d)(?<a2>\\d\\d)";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.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("分組二: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"));
- }
- }
結(jié)果展示
結(jié)論:由此可見()除了能將正則分組,還能按順序給出編號,從1開始。還可以給分組取名字,并根據(jù)名字獲取對應(yīng)匹配的值
非捕獲分組
1)如20(?:20|21|22)表示匹配2020|2021|2022
- @Test
- public void test2(){
- String str = "2021a2022B2023";
- String regStr = "20(?:20|21|22)";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示
2)如20(?=20|21|22)表示匹配2020或2021或2022中的20
- @Test
- public void test2(){
- String str = "2021a2022B2023";
- String regStr = "20(?=20|21|22)";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
這里匹配到的20,為2021和2022中的20
3)如20(?!20|21|22)表示匹配不匹配2020或2021或2022中的20,匹配其它20
- @Test
- public void test2(){
- String str = "2021a2022B2023";
- String regStr = "20(?!20|21|22)";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
這里匹配到的20為2023中的20
反向引用
- /**
- * 反向引用
- * 如果我們要找到一個字符串中連續(xù)4位威數(shù)字,并且第一位和第4位要相同,第二位和第三位相同。
- * 這時候我們使用反向引用就很簡單
- * 反向引用的內(nèi)部用法:\\n其中n代表分組號,如:字符串12345678870008,正則為(\\d)(\\d)\\2\\1
- * 反向引用的外部用法:$n其中n代表分組號
- */
字符串12345678870008,正則為(\\d)(\\d)\\2\\1
- @Test
- public void test2(){
- String str = "12345678870008";
- /**
- * 第一個(\\d)會分配的組為1
- * 第2個(\\d)會分配的組為2
- * \\2:表示引用組2的值,因此2和3的值就會相同
- * \\1:表示引用組1的值,因此1和4的值會相同
- */
- String regStr = "(\\d)(\\d)\\2\\1";
- Pattern compile = Pattern.compile(regStr);
- Matcher matcher = compile.matcher(str);
- while(matcher.find()){
- System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
- }
- }
結(jié)果展示