正則表達式RE

什麼是正則表達式

簡單的說,正則表達式就是處理字串的方法,他是以行爲單位來進行字串的處理行爲, 正則表達式通過一些特殊符號的輔助,可以讓使用者輕易的達到“搜尋/刪除/取代”某特定字串的處理程序!

正則表達式基本上是一種“表達式”, 只要工具程序支持這種表達式,那麼該工具程序就可以用來作爲正則表達式的字串處理之用。 例如 vi, grep, awk ,sed 等等工具,因爲她們有支持正則表達式, 所以,這些工具就可以使用正則表達式的特殊字符來進行字串的處理。但例如 cp, ls 等指令並未支持正則表達式, 所以就只能使用 bash 自己本身的通配符字符而已。

是 Linux 基礎當中的基礎,雖然也是最難的部分, 不過,如果學成了之後,一定是“大大的有幫助”的!這就好像是金庸小說裏面的學武難關:任督二脈! 打通任督二脈之後,武功立刻成倍成長!

關於語系

在英文大小寫的編碼順序中,zh_TW.big5 及 C 這兩種語系的輸出結果分別如下:

LANG=C 時:0 1 2 3 4 ... A B C D ... Z a b c d ...z
LANG=zh_TW 時:0 1 2 3 4 ... a A b B c C d D ... z Z

尤其要記住:

[:alnum] 代表所以的大小寫英文字符和數字 0-9 A—Z a-z
[:alpha:] 代表任意英文大小寫字符  A-Z a-z
[:lower:] 代表小寫字符       a-z
[:upper:] 代表大寫字符        A-Z
[:digit:] 代表數字         0-9

練習示例文件

數據來源於鳥哥私房菜


"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird



進階  grep

-A   n  把匹配成功行之後的n行也同時列出
-B   n  把匹配成功行之前的n行也同時列出

範例:

顯示 /etc/passwd 含有 mail 的行及其前2行和後 3 行

[root@e9818e4ea8b3 ~]# grep mail -B 2 -A3 /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

顯示 目標行的前後 3 行

 grep mail -C3 /etc/passwd

只顯示匹配到的字符

[root@e9818e4ea8b3 ~]# grep -o 'nologin' /etc/passwd

加上統計數量

[root@e9818e4ea8b3 ~]# grep -o -c 'nologin' /etc/passwd

只要文件名

[root@e9818e4ea8b3 ~]# grep -l 'nologin' /etc/passwd

遞歸查找,就是在一個目錄下查找

[root@e9818e4ea8b3 ~]# grep  -r  'nologin' /etc/

搜索 testtaste

[lf@x201t~]$ grep -n 't[ae]st' file1

搜索 oo 但其前面不要有g

[lf@x201t~]$grep -n '[^g]oo'  file1

注意:當搜索的行內含有要符合搜索條件時,此行就會忽略明確不要的條件,比如以上的例子就可能會搜索到下面的內容

3:tool is a good tool
8:goooooogle 

顯示oo 前面非小寫字符的行
方法一:

[lf@x201t~]$grep -n ‘[^a-z]oo’ file1

方法二:

[lf@x201t~]$grep -n  ‘[^[:lower:]]oo’ file1

顯示開否非英文字符的行

[lf@x201t~]$grep -n  ‘^[^[:alpha]]’ file1

符號 ^ 在 [] 內時是取反的意思,在 [] 之外是行首的意思
顯示行首不是#和;的行

[lf@x201t~]$grep '^[^#;]' file1

找到以 . 結尾的行

[lf@x201t~]$grep -n  ‘\.$’ file1

需要用 \ 進行轉意

查找 開頭是 g 和結尾也是 g ,中間的字符可有可無

[lf@x201t~]$grep -n   'g.*g' fiel1

. 代表一個任意字符
* 代表重複零到多個在 其前面的一個字符
.* 代表零個或多個任意字符

查找以a爲開頭的任意文件名
方法一:

[lf@x201t~]$ls -l a*

方法二:

[lf@x201t~]$ls |grep -n ‘^a.*’

列出 /etc 目錄下的鏈接文件

[lf@x201t~]$ls -l /etc |grep ‘^l’ 

再統計一下多少個

[lf@x201t~]$ls -l /etc |grep ‘^l’ |wc -l
···

查找僅含有2個o的字符串

[root@www ~]# grep -n 'o{2}' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
19:gooooogle yes!


由於沒有限定字符 o 後面的字符,所以會有19行的出現

查找含有2到4個o ,且以 g 結尾的字符串

[root@www ~]# grep -n 'go{2,4}g' regular_express.txt
18:google is the best tools for search keyword.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章