正則表達式是對字符串操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個“規則字符串”,這個“規則字符串”用來表達對字符串的一種過濾邏輯。
給定一個正則表達式和另一個字符串,我們可以達到如下的目的:
1. 給定的字符串是否符合正則表達式的過濾邏輯(稱作“匹配”);
2. 可以通過正則表達式,從字符串中獲取我們想要的特定部分。
正則表達式的特點是:
1. 靈活性、邏輯性和功能性非常的強;
2. 可以迅速地用極簡單的方式達到字符串的復雜控制。
3. 對于剛接觸的人來說,比較晦澀難懂。
由于正則表達式主要應用對象是文本,因此它在各種文本編輯器場合都有應用,小到著名編輯器EditPlus,大到Microsoft Word、Visual Studio等大型編輯器,都可以使用正則表達式來處理文本內容。
這里將會講到shell里邊的sed、awk、find、grep四劍客中的grep
–color=auto: 對匹配到的文本著色顯示; ?
-v: 顯示不能夠被pattern匹配到的行; ?
-i: 忽略字符大小寫 ?
-n:顯示匹配的行號 ?
-c: 統計匹配的行數 ?
-o: 僅顯示匹配到的字符串; ?
-q: 靜默模式,不輸出任何信息 ?
-A #: after, 后#行 ?
-B #: before, 前#行 ?
-C #:context, 前后各#行 ?
-e:實現多個選項間的邏輯or關系 grep –e ‘cat ’ -e ‘dog’ file ?
-w:整行匹配整個單詞 ?
-E:使用ERE
正則表達式元字符
字符匹配:
. : 匹配任意單個字符;
[] : 匹配指定范圍內的任意單個字符
[^] :匹配指定范圍外的任意單個字符
[:alpha:] 所有字母,包括大、小寫
[:alnum:] 所有字母和數字
[:upper:] 所有大寫字母
[:lower:] 所有小寫字母
[:digit:] 所有數字
[:punct:] 所有標點符號
[:space:] 空格和Tab
正則表達式
*:匹配前面的字符任意次,包括0次 貪婪模式:盡可能長的匹配
.*:任意長度的任意字符
\?:匹配其前面的字符0或1次
\+:匹配其前面的字符至少1次
\{n\}:匹配前面的字符n次
\{m,n\}:匹配前面的字符至少m次,至多n次
\{,n\}:匹配前面的字符至多n次
\{n,\}:匹配前面的字符至少n次
位置錨定:定位出現的位置
^:行首錨定,用于模式的最左側
$:行尾錨定,用于模式的最右側
^PATTERN$: 用于模式匹配整行
^$: 空行 ^[[:space:]]*$ :空白行
\< 或 \b:詞首錨定,用于單詞模式的左側
\> 或 \b:詞尾錨定;用于單詞模式的右側
\<PATTERN\>:匹配整個單詞
首先創建一個文本,內容如下;
[root@centous1 home]# cat regular_express.txt "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
1.搜尋特定字符串"the"
注: n為顯示行號
grep -n 'the' regular_express.txt
2.反向搜尋特定字符串"the"
grep -vn 'the' regular_express.txt
3.取得任意大小寫"the"的這個字符串
grep -in 'the' regular_express.txt
4.利用括號 [] 來搜尋集合字符
搜索test或taste這兩個單詞時,發現他們有共同的't?st',所以可以這么搜尋
grep -n 't[ae]st' regular_express.txt
這樣其實就是在找t[a]st和t[e]st這兩個分開的字符
如果搜索有 oo 的字符時,則可以使用:
grep -n 'oo' regular_express.txt
如果搜索oo時不想搜到 oo 前面有 g 的話,我們可以利用反向選擇[^]來達成:
grep -n '[^g]oo' regular_express.txt
如果搜索oo前面不想有小寫字符,則:
grep -n '[^a-z]oo' regular_express.txt
注: 大寫英文/小寫英文/數字 可以使用 [a-z]/[A-Z]/[0-9]等方式來書寫,也可以寫在一起
[a-zA-Z0-9]表示要求字符串是數字以及英文
如果我們要取得有數字的那行,則:
grep -n '[0-9]' regular_express.txt
注:但考慮到語系對編碼順序的影響,因此除了連續編碼使用減號[-]外,也可以用[:lower:]代替a-z 以及 [:digit:] 代替0-9 使用
grep -n '[^[:lower:]]oo' regular_express.txt
grep -n '[[:digit:]]' regular_express.txt
5.顯示行首為'the'的字符串
grep -n '^the' regular_express.txt
顯示行首是小寫字符
grep -n '^[a-z]' regular_express.txt
6.顯示行尾為點 . 的那一行
grep -n '\.$' regular_express.txt
7.顯示5-9行數據
cat -An regular_express.txt |head -n 10 |tail -n 6
8.顯示空白行
grep -n '^$' regular_express.txt
9.找出g??d字符串,起頭g結束d的四個字符串
grep -n 'g..d' regular_express.txt
10. o*代表空字符(就是有沒有字符都可以)或者一個到N個o字符,所以grep -n 'o*' regular_express.txt就會把所有行全部打印出來,
11.oo*代表o+空字符或者一個到N個o字符,所以grep -n 'oo*' regular_express.txt就會把o,oo,ooo等的行全部打印出來
12."goo*g"代表gog,goog,gooog…等
grep -n 'goo*g' regular_express.txt
13.找出含g…g字符串的行
注: .代表任意字符, .*則就代表空字符或者一個到N個任意字符
grep -n 'g.*g' regular_express.txt
14.找出含有數字的行
grep -n '[0-9][0-9]*' regular_express.txt
或grep -n '[0-9]' regular_express.txt
15.找出含兩個o的字符串
注:{}因為在shell里有特殊意義,所以需要加跳脫符\來讓其失去意義
grep -n 'o\{2\}' regular_express.txt
找出g后含2到5個o然后以g結尾的字符串
grep -n 'go\{2,5\}g' regular_express.txt
找出g后含2以上的o然后以g結尾的字符串
grep -n 'go\{2,\}g' regular_express.txt
原創文章,作者:forest,如若轉載,請注明出處:http://www.www58058.com/30509