Linux shell 正則表達式

簡介

正則表達式 (regular expression) 描述了一種字符串匹配的模式(pattern),可以用來檢查一個串是否含有某種子串、將匹配的子串替換或者從某個串中取出符合某個條件的子串等。

常用正則表達式

常用字符

1. 普通字符

普通字符主要講解以下內容,並舉例說明

//        String regStr = "[a-z]";//匹配a-z中任意一個字符
//        String regStr = "[A-Z]";//匹配A-Z中任何一個字符
//        String regStr = "abc";//匹配字符串abc
//        String regStr = "(?i)abc";//匹配字母abc不區分大小寫
//        String regStr = "[0-9]";//匹配0-9任何一個字符
//        String regStr = "[^0-9]";//匹配不是0-9中的任何一個字符
//        String regStr = "[^0-9]{2}";//匹配2個不是0-9的字符
//        String regStr = "\\d";//匹配任何一個數字字符,等價於[0-9]
//        String regStr = "\\D";//匹配任何一個非數字字符,等價於[^0-9]
//        String regStr = "\\w";//匹配任何一個數字、字母、下劃線,等價於[0-9a-zA-Z_]
//        String regStr = "\\W";//匹配任何一個除了數字、字母、下劃線,等價於[^0-9a-zA-Z_]
//        String regStr = "\\s";//匹配任何一個空字符
//        String regStr = "\\S";//匹配任何一個非空字符
//        String regStr = "ab|cd";//選擇匹配符,匹配字符串ab或者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("匹配到的數據爲:"+matcher.group(0));
        }
  }

結果展示

  1. 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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

3)String regStr = "abc";// 匹配字符串 abc

@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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

4)String regStr = "(?i)abc";// 匹配字母 abc 不區分大小寫

@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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

  1. 限定符
/**
 * 限定符
 *  *:表示出現任意次數,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次,如[0-9]{2,4}表示匹配次數2-4次數字
 */
  1. *:表示出現任意次數,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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

2)+:表示出現至少 1 次或者 n 次,如(abc)+ 表示 abc 出現 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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

3)?:表示出現至少 0 次或者 1 次,如 abc?表示 c 出現 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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

4){n}: 表示出現 n 次,如 [0-9]{2}, 表示匹配 2 次數字

@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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

5){n,}表示至少出現 n 次,如 [0-9]{3,} 表示匹配至少 3 次數字

@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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

6){n,m}表示出現至少 n 次,最多 m 次,如 [0-9]{2,4} 表示匹配次數 2-4 次數字

@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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

  1. 定位符
/**
 * 定位符
 * ^:表示字符串以什麼開頭的意思。如:有一個字符串123abc,正則爲^[0-9]+[a-z]*(必須已數字開頭),則能成功匹配上。如果字符串爲a123abc則匹配不上
 * $:表示字符串以什麼結束的意思。如:有一個字符串123abc,正則爲^[0-9]+[a-z]+$(表示以數字開頭,字母結尾)則能成功匹配上。如果字符串爲a123abc1則匹配不上
 * \\b:表示邊緣匹配(字符串的結尾或者空格之後)。有一個字符串abc123abc,正則爲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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

  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("匹配到的數據爲:"+matcher.group(0));
    }
}

沒有匹配到,因爲要以數字結束

  1. \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("匹配到的數據爲:"+matcher.group(0));
    }
}

匹配到的是最後一個 “zyp”

  1. \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("匹配到的數據爲:"+matcher.group(0));
    }
}

匹配到的是第一個 “zyp”

  1. 分組
/**
 * 分組:可分爲捕獲分組和非捕獲分組
 * 1.捕獲分組:
 * 1)如(\\d\\d)(\\d\\d)表示匹配4位數字,如果字符串位2021abcd,
 * 我們通過matcher.group(0)得到2021
 * 通過matcher.group(1)得到20
 * 通過matcher.group(2)得到21
 * 由此可見()起到分組的作用
 *
 * 2)如(?<a1>\\d\\d)(?<a2>\\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)表示匹配2020或2021或2022中的20
 *  3)如20(?!20|21|22)表示匹配不匹配2020或2021或2022中的20,匹配其它20
 *
 */

捕獲分組

1)如(\d\d)(\d\d) 表示匹配 4 位數字,如果字符串爲 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));
    }
}

結果展示

結論:由此可見 () 會將正則分組,並按順序給出編號,從 1 開始

2)(?\d\d)(?\d\d) 表示匹配 4 位數字,如果字符串位 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"));
    }
}

結果展示

結論:由此可見 () 除了能將正則分組,還能按順序給出編號,從 1 開始。還可以給分組取名字,並根據名字獲取對應匹配的值

非捕獲分組

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("匹配到的數據爲:"+matcher.group(0));
    }
}

結果展示

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("匹配到的數據爲:"+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("匹配到的數據爲:"+matcher.group(0));
    }
}

這裏匹配到的 20 爲 2023 中的 20

  1. 反向引用
/**
 * 反向引用
 * 如果我們要找到一個字符串中連續4位威數字,並且第一位和第4位要相同,第二位和第三位相同。
 * 這時候我們使用反向引用就很簡單
 * 反向引用的內部用法:\\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("匹配到的數據爲:"+matcher.group(0));
        }
    }

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/xFIvYJX60TQvZZYJ8z0gng