Linux Shell 正则表达式

系统 Linux
正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

 简介

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

常用正则表达式

常用字符

普通字符

普通字符主要讲解以下内容,并举例说明

 

//        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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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("匹配到的数据为:"+matcher.group(0));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

限定符 

/**  
 * 限定符  
 *  *:表示出现任意次数,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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

定位符 

/**  
 * 定位符  
 * ^:表示字符串以什么开头的意思。如:有一个字符串123abc,正则为^[0-9]+[a-z]*(必须已数字开头),则能成功匹配上。如果字符串为a123abc则匹配不上  
 * $:表示字符串以什么结束的意思。如:有一个字符串123abc,正则为^[0-9]+[a-z]+$(表示以数字开头,字母结尾)则能成功匹配上。如果字符串为a123abc1则匹配不上  
 * \\b:表示边缘匹配(字符串的结尾或者空格之后)。有一个字符串abc123abc,正则为abc\\b,匹配到的为最后的那个abc  
 * \\B:与\\b相反  
 */ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

2) $:表示字符串以什么结束的意思 

@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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

没有匹配到,因为要以数字结束

3) \\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)); 
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

匹配到的是最后一个“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("匹配到的数据为:"+matcher.group(0)); 
   }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

匹配到的是第一个“zyp”

分组 

/**  
 * 分组:可分为捕获分组和非捕获分组  
 * 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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

捕获分组

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.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

结果展示

结论:由此可见()会将正则分组,并按顺序给出编号,从1开始

2) (?<a1>\\d\\d)(?<a2>\\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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

结果展示

结论:由此可见()除了能将正则分组,还能按顺序给出编号,从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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

结果展示

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

这里匹配到的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));  
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

这里匹配到的20为2023中的20

反向引用 

/**  
 * 反向引用  
 * 如果我们要找到一个字符串中连续4位威数字,并且第一位和第4位要相同,第二位和第三位相同。  
 * 这时候我们使用反向引用就很简单  
 * 反向引用的内部用法:\\n其中n代表分组号,如:字符串12345678870008,正则为(\\d)(\\d)\\2\\1  
 * 反向引用的外部用法:$n其中n代表分组号  
 */ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

字符串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)); 
       }  
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

结果展示 

 

责任编辑:庞桂玉 来源: 良许Linux
相关推荐

2017-05-12 10:47:45

Linux正则表达式程序基础

2018-09-27 15:25:08

正则表达式前端

2020-09-04 09:16:04

Python正则表达式虚拟机

2020-11-16 11:10:00

ShellLinux正则表达式

2024-09-14 09:18:14

Python正则表达式

2010-03-03 13:09:10

Linux正则表达式

2010-03-03 12:53:50

Linux正则表达式

2010-03-03 12:58:41

Linux正则表达式

2010-03-03 10:51:32

正则表达式

2010-03-03 11:03:51

Linux正则表达式

2010-03-03 13:31:25

Linux正则表达式

2010-03-25 18:25:36

Python正则表达式

2009-09-16 18:19:34

正则表达式组

2021-01-27 11:34:19

Python正则表达式字符串

2019-07-17 15:45:47

正则表达式字符串前端

2022-03-28 06:19:14

正则表达式开发

2011-06-02 12:34:16

正则表达式

2009-02-18 09:48:20

正则表达式Java教程

2009-09-16 17:15:57

正则表达式引擎

2016-11-10 16:21:22

Java 正则表达式
点赞
收藏

51CTO技术栈公众号