首頁  >  文章  >  php教程  >  grep用法詳解 grep與正規表示式

grep用法詳解 grep與正規表示式

高洛峰
高洛峰原創
2016-12-13 14:28:031160瀏覽

正規表示式只是一種表示法,只要工具支援這種表示法, 那麼該工具就可以處理正規表示式的字串。 vim、grep、awk 、sed 都支援正規表示式,也正是因為由於它們支援正規,才顯得它們強大;在以前上班的公司裡,由於公司是基於web的服務型網站(nginx),對正則的需求比較大,所以也花了點時間研究正規,特與大家分享下: 

1基礎正規表示式 
grep 工具,以前介紹過。
grep -[acinv] '搜尋內容字串' filename 
-a 以文字檔案方式搜尋 
-c 計算找到的符合行的次數 
-i 忽略大小寫 
-n 順便輸出行號 
-v 反向選擇,即找沒有搜索字符串的行 
其中搜索串可以是正則表達式! 


搜索有the的行,並輸出行號 
$grep -n 'the' regular_express.txt 
搜索沒有the的行,並且輸出行號 
$grep -nv 'the' regular_express.txt 

2 利用[]搜尋集合字符 
[] 表示其中的某一個字符,例如[ade] 表示a或d或e 
woody@xiaoc:~ /tmp$ grep -n 't[ae]st' regular_express.txt 
8:I can't finish the test. 
9:Oh! the soup taste good! 

可以用^符號做[]內的前綴,表示除[]內的字元之外的字元。
例如搜尋oo前沒有g的字串所在的行. 使用'[^g]oo' 作搜尋字串 
woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt 
2 :apple is my favorite food. 
3:Football game is not use feet only. 
18:google is the best tools for search keyword. 
19:goooooogle yes! 表示小寫字母,[0-9] 表示0~9的數字, [A-Z] 則是大寫字母們。 [a-zA-Z0-9]表示所有數字與英文字元。 當然也可以配合^來排除字元。
搜尋包含數字的行 
woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt 
5:However ,this dress is about $ 3183 dollars.u
15: Youare the bare you are the no.1. 

行首與行尾字符^ $. ^ 表示行的開頭,$表示行的結尾( 不是字符,是位置)那麼'^$' 就表示空行,因為只有 
行首和行尾。 
這裡^與[]裡面使用的^意義不同。它表示^後面的串是在行的開頭。
例如搜尋the在開頭的行 
woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt 
12:the symbol '*' is represented as star. 

搜尋以寫字母開頭的行woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt 
2:apple is my favorite food. 
4:this dress doesn't me.
10:motorcy is 12:the symbol '*' is represented as star. 
18:google is the best tools for search keyword. 
19:goooooogle yes! 
20:go! go! Let's go.mocinoo~
搜尋開頭不是英文字母的行 
woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs. 
21:#I am VBird 
woody@xiaoc:~/tmp$ 

$表示它前面的串是在行的結尾,例如'.' 表示. 在一行的結尾 
搜尋末尾是.的行 
woody@xiaoc :~/tmp$ grep -n '.$' regular_express.txt //. 是正規表示式的特殊符號,所以要用轉義 
1:"Open Source" is a good mechanism to develop programs. 
2:apple is my favorite food. 
3:Football game is not use feet only. 
4:this dress doesn't fit me. 
5:However ,this dress is about $ 3183 d . 
..... 

注意在MS的系統下產生的文字文件,換行會加上一個^M 字元。所以最後的字元會是隱藏的^M ,在處理Windows 
下面的文字時要特別注意! 
可以用cat dos_file | tr -d 'r' > unix_file 來刪除^M符號。 ^M==r 

那麼'^$' 就表示只有行首行尾的空行拉!
搜尋空白行 
woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt 
22: 
23: 
woody@xiaoc: /tmp$ 
23: 
woody@xiaoc: /tmp$ 
23: 
woody@xiaoc: /tmp$ 

/tmp$ grep -vn '^$' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs. 
2:apple is my favorite food. 
3:Footo is not. :this dress doesn't fit me. 
.......... 

任一個字元. 與重複字元* 

在bash中*代表通配符,用來代表任意個 字符,但是在正則表達式中,他含義不同,*表示有0個或多個 某個字符。 
例如 oo*, 表示第一個o一定存在,第二個o可以有一個或多個,也可以沒有,因此代表至少一個o. 

點. 代表一個任意字符,必須存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。

woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs. 
9:Oh! the good mechanism to develop programs. 
9:Oh! the up taste good! The world is the same with 'glad'. 
woody@xiaoc:~/tmp$ 

搜尋兩個o以上的字串 
woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前兩個o一定存在,第三個o可沒有,也可有多個。
1:"Open Source" is a good mechanism to develop programs. 
2:apple is my favorite food. 
3:Football game is not use feet only. 
3:Football game is not use feet only. 
9:Oh! the up the best tools for search keyword. 
19:goooooogle yes! 

搜尋g開頭和結尾,中間是至少一個o的字串,即gog, goog....gooog...等 
woody@xiaoc:~/ tmp$ grep -n 'goo*g' regular_express.txt 
18:google is the best tools for search keyword. 
19:goooooogle yes! 

搜尋g開頭和結尾的字串在的行 /tmp$ grep -n 'g.*g' regular_express.txt // .*表示0個或多個任意字元 
1:"Open Source" is a good mechanism to develop programs. 
14:The gd software is a library for drafting programs. 
18:google is the best tools for search keyword. 
19:goooooogle yes! 
20:go! go! Let's go. 

20:go! go! Let's go. 

0個或多個, 如果要確切的限製字元重複數量,就用{範圍} 。範圍是數字用,隔開2,5 表示2~5個, 
2表示2個,2, 表示2到更多個 
注意,由於{ }在SHELL中有特殊意義,因此作為正規表達式用的時候要用轉義一下。 

搜尋包含兩個o的字串的行。
woody@xiaoc:~/tmp$ grep -n 'o{2}' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs. 
2:apple is my favorite fooded is not use feet only. 
9:Oh! the soup taste good! 
18:google is the best tools for search keyword. 
19:goooooogle yes! 

搜索g的字串的行。
woody@xiaoc:~/tmp$ grep -n 'go{2,5}g' regular_express.txt 
18:google is the best tools for search keyword. 


搜尋包含g後面跟2個以上再跟g的行。 。
woody@xiaoc:~/tmp$ grep -n 'go{2,}g' regular_express.txt 
18:google is the best tools for search keyword. 
19:gggle yes! 

的^ - 不表現特殊意義,可以放在[]裡面內容的後面。 
'[^a-z.!^ -]' 表示沒有小寫字母,沒有. 沒有!, 沒有空格,沒有- 的 串,注意[]裡面有個小空格。 

另外shell 裡面的反向選擇為[!range], 正則裡面是 [^range] 


2擴展正則表達式 

擴展正則表達式是對基礎正則表達式添加了幾個特殊構成的。 
它令某些操作更加方便。
例如我們要去除空白行和行首為#的行, 會這樣用: 
woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#' 
"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. 
然而...... 
使用支援擴展正規表示式的egrep 與擴展特殊符號| ,會方便許多。 
注意grep只支援基礎表達式, 而egrep 支援擴充的, 其實 egrep 是 grep -E 的別名而已。因此grep -E 支援擴展正則。
那麼: 
woody@xiaoc:~/tmp$ egrep -v '^$|^#' regular_express.txt 
"Open Source" is a good mechanism to develop programs. 
apple is my voal use feet only. 
this dress doesn't fit me. 
.................... 
這裡| 表示或的關係。 即滿足 ^$ 或 ^# 的字串。 

這裡列出幾個擴展特殊符號: 
+, 於 . * 作用類似,表示 一個或多個重複字符。 
?, 於 . * 作用類似,表示0個或一個字符。 
|,表示或關係,如 'gd|good|dog' 表示有gd,good或dog的串 
(),將部分內容合成一個單元組。 例如 要搜尋 glad 或 good 可以這樣 'g(la|oo)d' 
()的好處是可以對小組使用 + ? * 等。 
例如要搜尋A和C開頭結尾,中間有至少一個(xyz) 的串,可以這樣 : 'A(xyz)+C'

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn