正則表達式分類: 1、基本正則表達式(BRE,Basic Regular Expression) BRE對應的元字符有 "^%.[]*" 2、擴展正則表達式(ERE,Extended Regular Expression) ERE在BRE的基礎上增長了 "(){}?+|" 等 說明:支持擴展正則的3中方法: (1)grep命令加"-E"參數。 (2)grep命令不加參數也能夠使用擴展正則表達式的特殊字符,可是須要在使用每一個特殊的字符前面加「\」(反斜線)。 (3)egrep命令能夠直接支持擴展正則。 基本正則表達式(BRE)集合 ^ 用法爲: "^abc",表示匹配以abc單詞開頭的行 $ 用法爲: "abc$",表示匹配以abc單詞結尾的行 ^$ 表示空行,以"^"結尾的行,或者以"$"開頭的行 . 表示匹配任意一個且只有一個字符(可是不能匹配空行) \ 轉移字符,讓有特殊含義的字符表達原來的含義,如"\." 只表示小數點 * 匹配前一個字符(連續出現)0次或者1次以上。注意,當重複0次的時候,表示什麼也沒有(空),即匹配全部內容 .* 匹配全部內容 ^.* 匹配以任意多個字符開頭的內容 .*$ 匹配以任意多個字符結尾的內容 [abc] 匹配"[]"集合內任意一個字符a或b或c,[abc]也能夠寫成[a-c] [^abc] 匹配不包含"^"後的任意字符a或b或c,此處的"^"不能用"!"替代 擴展正則表達式(ERE)集合 + 匹配前一個字符1次或屢次 [:/]+ 匹配括號內的":"或"/"字符一次或屢次 ? 匹配前一個字符0次或1次 | 表示或者,即同時過濾多個字符串 () 分組過濾,被括起來的內容表示一個總體,另外"()"的內容能夠被後面的"\n"引用,n爲數字,表示引用第幾個括號的內容。 \n 引用前面"()"裏面的內容,例如,(aa)\1,匹配aaaa a{n,m} 匹配前一個字符最少n次,最多m次 a{n,} 匹配前已給字符最少n次 a{n} 匹配前一個字符恰好n次 a{,m} 匹配前一個字符最多m次 基本正則表達式實踐 實踐環境準備 mkdir ~/test -p cat >~/test/guojun.txt <<EOF I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! our site is https://www.cnblogs.com my qq num is 12000345. not 1200000345. my god ,i am not guojnn,but GUOJUN! EOF 範例:"^" 功能實踐 輸出以m開頭的全部行並打印行號 [root@testdb62 test]# grep -n "^m" guojun.txt 6:my qq num is 12000345. 9:my god ,i am not guojnn,but GUOJUN! 範例:"$" 功能實踐 輸出以m結尾的全部行 [root@testdb62 test]# grep "m$" guojun.txt our site is https://www.cnblogs.com 範例:"$" 功能實踐 顯示空行及對應的行號 [root@testdb62 test]# grep -n "^$" guojun.txt 3: 7: 範例:"." 功能實踐 匹配任意一個字符並輸出對應文件中的行號 [root@testdb62 test]# grep -n "." guojun.txt 1:I am guojun teacher! 2:I teach Database. 4:I like badminton ball ,billiard ball and chinese chess! 5:our site is https://www.cnblogs.com 6:my qq num is 12000345. 8:not 1200000345. 9:my god ,i am not guojnn,but GUOJUN! 範例:"." 和"\"功能實踐 顯示以"." 結尾的行 [root@testdb62 test]# grep '\.$' guojun.txt I teach Database. my qq num is 12000345. not 1200000345. 範例:".*" 功能實踐 輸出文件中連續出現0次或0次以上的數字內容 [root@testdb62 test]# grep '0*' guojun.txt I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! our site is https://www.cnblogs.com my qq num is 12000345. not 1200000345. my god ,i am not guojnn,but GUOJUN! 範例:".*" 功能實踐 經過grep顯示文件的全部內容,包括空行 [root@testdb62 test]# grep '.*' guojun.txt I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! our site is https://www.cnblogs.com my qq num is 12000345. not 1200000345. my god ,i am not guojnn,but GUOJUN! 範例:測試 "^.*o" 可以匹配到的內容 [root@testdb62 test]# grep '^.*o' guojun.txt I am guojun teacher! I like badminton ball ,billiard ball and chinese chess! our site is https://www.cnblogs.com not 1200000345. my god ,i am not guojnn,but GUOJUN! 範例:匹配出文件中全部的大寫字母 [root@testdb62 test]# grep '[A-Z]' guojun.txt I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! my god ,i am not guojnn,but GUOJUN! 範例:匹配除了小寫字母之外的符號 [root@testdb62 test]# grep '[^a-z]' guojun.txt I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! our site is https://www.cnblogs.com my qq num is 12000345. not 1200000345. my god ,i am not guojnn,but GUOJUN! 擴展正則表達式實踐 範例:匹配文件中的數字0一次或屢次 [root@testdb62 test]# egrep "0+" guojun.txt my qq num is 12000345. not 1200000345. 範例:顯示文件中包含gd或god的行 新建測試文件 mkdir ~/test -p cat >~/test/gd.txt <<EOF good glad gd god goood EOF [root@testdb62 test]# cat gd.txt good glad gd god goood [root@testdb62 test]# egrep 'go?d' gd.txt gd god 範例:取出包含3306或1521的行 [root@testdb62 test]# egrep '3306|1521' /etc/services mysql 3306/tcp # MySQL mysql 3306/udp # MySQL ncube-lm 1521/tcp # nCube License Manager ncube-lm 1521/udp # nCube License Manager 範例:顯示文件中包含good或glad的行 [root@testdb62 test]# egrep 'g(oo|la)d' gd.txt good glad 範例:重複前一個字符各類次數的例子 [root@testdb62 test]# egrep "0{3,5}" guojun.txt # 匹配數字0,3到5次 my qq num is 12000345. not 1200000345. [root@testdb62 test]# egrep "0{,5}" guojun.txt # 匹配數字0,最多5次,所有輸出了 I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! our site is https://www.cnblogs.com my qq num is 12000345. not 1200000345. my god ,i am not guojnn,but GUOJUN! [root@testdb62 test]# egrep "0{3,}" guojun.txt # 匹配數字0,最少3次 my qq num is 12000345. not 1200000345. [root@testdb62 test]# egrep "0{3}" guojun.txt # 匹配數字0,3次 my qq num is 12000345. not 1200000345. 預約義特殊中括號表達式 描述:匹配任意一個字母或者數字至關於[a-zA-Z0-9] [:alnum:] 匹配任意一個字母或者數字至關於[a-zA-Z0-9] [:alpha:] 匹配任意一個字母至關於[a-zA-Z] [:blank:] 匹配空格與製表符(橫向與縱向) [:upper:] 匹配大寫 [:lower:] 匹配小寫 [:punct:] 匹配標點符號 [:space:] 匹配包括換行符、回車在內的全部空白符 [:digit:] 匹配任意一個數字至關於[0-9] [:graph:] 匹配任意一個能夠看得見且能夠打印的字符 [:xdigit:] 匹配任何一個16進制數至關於[0-9][a-f][A-F] [:cntrl:] 任何一個控制符(ASCII字符集的前32爲字符) [:print:] 任何一個能夠打印的字符 範例:括號表達式使用示例 [root@testdb62 test]# grep "[[:digit:]]" guojun.txt my qq num is 12000345. not 1200000345. [root@testdb62 test]# grep "[[:upper:]]" guojun.txt I am guojun teacher! I teach Database. I like badminton ball ,billiard ball and chinese chess! my god ,i am not guojnn,but GUOJUN!