自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux Shell 正則表達式

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

 簡介

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

常用正則表達式

常用字符

普通字符

普通字符主要講解以下內(nèi)容,并舉例說明

 

  1. //        String regStr = "[a-z]";//匹配a-z中任意一個字符  
  2. //        String regStr = "[A-Z]";//匹配A-Z中任何一個字符  
  3. //        String regStr = "abc";//匹配字符串a(chǎn)bc  
  4. //        String regStr = "(?i)abc";//匹配字母abc不區(qū)分大小寫  
  5. //        String regStr = "[0-9]";//匹配0-9任何一個字符  
  6. //        String regStr = "[^0-9]";//匹配不是0-9中的任何一個字符  
  7. //        String regStr = "[^0-9]{2}";//匹配2個不是0-9的字符  
  8. //        String regStr = "\\d";//匹配任何一個數(shù)字字符,等價于[0-9]  
  9. //        String regStr = "\\D";//匹配任何一個非數(shù)字字符,等價于[^0-9]  
  10. //        String regStr = "\\w";//匹配任何一個數(shù)字、字母、下劃線,等價于[0-9a-zA-Z_]  
  11. //        String regStr = "\\W";//匹配任何一個除了數(shù)字、字母、下劃線,等價于[^0-9a-zA-Z_]  
  12. //        String regStr = "\\s";//匹配任何一個空字符  
  13. //        String regStr = "\\S";//匹配任何一個非空字符  
  14. //        String regStr = "ab|cd";//選擇匹配符,匹配字符串a(chǎn)b或者cd 

1) String regStr = "[a-z]";//匹配a-z中任意一個字符 

  1. @Test  
  2. public void test1() {  
  3.        String str = "abc2021" 
  4.        String regStr = "[a-z]" 
  5.        Pattern compile = Pattern.compile(regStr);  
  6.        Matcher matcher = compile.matcher(str);  
  7.        while(matcher.find()){  
  8.            System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.        }  
  10.  } 

結(jié)果展示

2) String regStr = "[A-Z]";//匹配A-Z中任何一個字符 

  1. @Test  
  2. public void test2(){  
  3.     String str = "ABCabc2021" 
  4.     String regStr = "[A-Z]" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

3)String regStr = "abc";//匹配字符串a(chǎn)bc 

  1. @Test  
  2. public void test2(){  
  3.     String str = "ABCabc2021" 
  4.     String regStr = "abc" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

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

  1. @Test  
  2. public void test2(){  
  3.     String str = "ABCabc2021" 
  4.     String regStr = "(?i)abc"
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

5) String regStr = "[0-9]";//匹配0-9任何一個字符 

  1. @Test  
  2. public void test2(){  
  3.     String str = "ABCabc2021" 
  4.     String regStr = "[0-9]" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

6) String regStr = "[^0-9]";//匹配不是0-9中的任何一個字符 

  1. @Test  
  2. public void test2(){  
  3.     String str = "ABCabc2021" 
  4.     String regStr = "[^0-9]" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

限定符 

  1. /**  
  2.  * 限定符  
  3.  *  *:表示出現(xiàn)任意次數(shù),0次或者n次,如(abc)*表示abc出現(xiàn)0次或者多次  
  4.  *  +:表示出現(xiàn)至少1次或者n次,如(abc)+表示abc出現(xiàn)1次或者多次  
  5.  *  ?:表示出現(xiàn)至少0次或者1次,如abc?表示c出現(xiàn)0次或者1次  
  6.  *  {n}:表示出現(xiàn)n次,如[0-9]{2},表示匹配2次數(shù)字  
  7.  *  {n,}表示至少出現(xiàn)n次,如[0-9]{3,}表示匹配至少3次數(shù)字  
  8.  *  {n,m}表示出現(xiàn)至少n次,最多m次,如[0-9]{2,4}表示匹配次數(shù)2-4次數(shù)字  
  9.  */ 

1) *:表示出現(xiàn)任意次數(shù),0次或者n次 

  1. @Test  
  2. public void test2(){  
  3.     String str = "zypabcabc2021" 
  4.     String regStr = "zyp(abc)*" 
  5.     Pattern compile = Pattern.compile(regStr); 
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

2)+:表示出現(xiàn)至少1次或者n次,如(abc)+表示abc出現(xiàn)1次或者多次 

  1. @Test  
  2. public void test2(){  
  3.     String str = "zypabc2021" 
  4.     String regStr = "zyp(abc)+" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

3)?:表示出現(xiàn)至少0次或者1次,如abc?表示c出現(xiàn)0次或者1次 

  1. @Test  
  2. public void test2(){  
  3.     String str = "zyp2021" 
  4.     String regStr = "zyp(abc)?" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

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

  1. @Test  
  2. public void test2(){  
  3.     String str = "zyp2021" 
  4.     String regStr = "[0-9]{2}" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

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

  1. @Test  
  2. public void test2(){  
  3.     String str = "zyp2021" 
  4.     String regStr = "[0-9]{2,}" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));
  9.     }  

結(jié)果展示

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

  1. @Test  
  2. public void test2(){  
  3.     String str = "zyp2021" 
  4.     String regStr = "[0-9]{2,4}" 
  5.     Pattern compile = Pattern.compile(regStr); 
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

定位符 

  1. /**  
  2.  * 定位符  
  3.  * ^:表示字符串以什么開頭的意思。如:有一個字符串123abc,正則為^[0-9]+[a-z]*(必須已數(shù)字開頭),則能成功匹配上。如果字符串為a123abc則匹配不上  
  4.  * $:表示字符串以什么結(jié)束的意思。如:有一個字符串123abc,正則為^[0-9]+[a-z]+$(表示以數(shù)字開頭,字母結(jié)尾)則能成功匹配上。如果字符串為a123abc1則匹配不上  
  5.  * \\b:表示邊緣匹配(字符串的結(jié)尾或者空格之后)。有一個字符串a(chǎn)bc123abc,正則為abc\\b,匹配到的為最后的那個abc  
  6.  * \\B:與\\b相反  
  7.  */ 

1) ^:表示字符串以什么開頭的意思 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021zyp" 
  4.     String regStr = "^[0-9]+" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

2) $:表示字符串以什么結(jié)束的意思 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021zyp"; 
  4.     String regStr = "[0-9]$" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){ 
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

沒有匹配到,因為要以數(shù)字結(jié)束

3) \\b:表示邊緣匹配(字符串的結(jié)尾或者空格之后) 

  1. @Test  
  2. public void test2(){  
  3.     String str = "zyp2021zyp" 
  4.     String regStr = "zyp\\b" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0)); 
  9.     }  

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

4) \\B:與\\b相反 

  1. @Test  
  2. public void test2(){  
  3.     String str = "zyp2021zyp" 
  4.     String regStr = "zyp\\B" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0)); 
  9.    }  

匹配到的是第一個“zyp”

分組 

  1. /**  
  2.  * 分組:可分為捕獲分組和非捕獲分組  
  3.  * 1.捕獲分組:  
  4.  * 1)如(\\d\\d)(\\d\\d)表示匹配4位數(shù)字,如果字符串位2021abcd,  
  5.  * 我們通過matcher.group(0)得到2021  
  6.  * 通過matcher.group(1)得到20  
  7.  * 通過matcher.group(2)得到21  
  8.  * 由此可見()起到分組的作用  
  9.  *  
  10.  * 2)如(?<a1>\\d\\d)(?<a2>\\d\\d)表示匹配4位數(shù)字,如果字符串位2021abcd,  
  11.  * 我們通過matcher.group(0)得到2021  
  12.  * 通過matcher.group(1)得到20,還可以通過matcher.group(a1)得到20  
  13.  * 通過matcher.group(2)得到21,還可以通過matcher.group(a2)得到21  
  14.  * 由此可見()起到分組的作用  
  15.  *  
  16.  * 2.非捕獲分組:不能通過group(1)或者group(2)獲取值  
  17.  *  1)如20(?:20|21|22)表示匹配2020|2021|2022  
  18.  *  2) 如20(?=20|21|22)表示匹配2020或2021或2022中的20  
  19.  *  3)如20(?!20|21|22)表示匹配不匹配2020或2021或2022中的20,匹配其它20  
  20.  *  
  21.  */ 

捕獲分組

1)如(\\d\\d)(\\d\\d)表示匹配4位數(shù)字,如果字符串為2021abcd, 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021abcd" 
  4.     String regStr = "(\\d\\d)(\\d\\d)" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("matcher.group(0):"+matcher.group(0));  
  9.         System.out.println("分組一:matcher.group(1):"+matcher.group(1));  
  10.         System.out.println("分組二:matcher.group(2):"+matcher.group(2));  
  11.     }  

結(jié)果展示

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

2) (?<a1>\\d\\d)(?<a2>\\d\\d)表示匹配4位數(shù)字,如果字符串位2021abcd 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021abcd" 
  4.     String regStr = "(?<a1>\\d\\d)(?<a2>\\d\\d)" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("matcher.group(0):"+matcher.group(0));  
  9.         System.out.println("分組一:matcher.group(1):"+matcher.group(1));  
  10.         System.out.println("分組二:matcher.group(2):"+matcher.group(2));  
  11.         System.out.println("分組名a1:matcher.group(1):"+matcher.group("a1"));  
  12.         System.out.println("分組名a2:matcher.group(2):"+matcher.group("a2"));  
  13.     }  

結(jié)果展示

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

非捕獲分組

1)如20(?:20|21|22)表示匹配2020|2021|2022 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021a2022B2023" 
  4.     String regStr = "20(?:20|21|22)" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

結(jié)果展示

2)如20(?=20|21|22)表示匹配2020或2021或2022中的20 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021a2022B2023" 
  4.     String regStr = "20(?=20|21|22)" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     }  

這里匹配到的20,為2021和2022中的20

3)如20(?!20|21|22)表示匹配不匹配2020或2021或2022中的20,匹配其它20 

  1. @Test  
  2. public void test2(){  
  3.     String str = "2021a2022B2023" 
  4.     String regStr = "20(?!20|21|22)" 
  5.     Pattern compile = Pattern.compile(regStr);  
  6.     Matcher matcher = compile.matcher(str);  
  7.     while(matcher.find()){  
  8.         System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0));  
  9.     } 

這里匹配到的20為2023中的20

反向引用 

  1. /**  
  2.  * 反向引用  
  3.  * 如果我們要找到一個字符串中連續(xù)4位威數(shù)字,并且第一位和第4位要相同,第二位和第三位相同。  
  4.  * 這時候我們使用反向引用就很簡單  
  5.  * 反向引用的內(nèi)部用法:\\n其中n代表分組號,如:字符串12345678870008,正則為(\\d)(\\d)\\2\\1  
  6.  * 反向引用的外部用法:$n其中n代表分組號  
  7.  */ 

字符串12345678870008,正則為(\\d)(\\d)\\2\\1 

  1. @Test  
  2.     public void test2(){  
  3.         String str = "12345678870008" 
  4.         /**  
  5.          * 第一個(\\d)會分配的組為1  
  6.          * 第2個(\\d)會分配的組為2  
  7.          * \\2:表示引用組2的值,因此2和3的值就會相同  
  8.          * \\1:表示引用組1的值,因此1和4的值會相同  
  9.          */  
  10.         String regStr = "(\\d)(\\d)\\2\\1" 
  11.         Pattern compile = Pattern.compile(regStr);  
  12.         Matcher matcher = compile.matcher(str);  
  13.         while(matcher.find()){  
  14.             System.out.println("匹配到的數(shù)據(jù)為:"+matcher.group(0)); 
  15.        }  
  16.     } 

結(jié)果展示 

 

責任編輯:龐桂玉 來源: 良許Linux
相關(guān)推薦

2017-05-12 10:47:45

Linux正則表達式程序基礎(chǔ)

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 11:03:51

Linux正則表達式

2010-03-03 13:31:25

Linux正則表達式

2010-03-03 10:51:32

正則表達式

2010-03-25 18:25:36

Python正則表達式

2021-01-27 11:34:19

Python正則表達式字符串

2009-02-18 09:48:20

正則表達式Java教程

2009-09-16 18:19:34

正則表達式組

2011-06-02 12:34:16

正則表達式

2019-07-17 15:45:47

正則表達式字符串前端

2022-03-28 06:19:14

正則表達式開發(fā)

2016-11-10 16:21:22

Java 正則表達式

2023-09-13 08:12:45

點贊
收藏

51CTO技術(shù)棧公眾號