8.正規表示法
本章同步視頻:https://edu.51cto.com/sd/e4874
8.1 正規表示法
正規表示法就是處理字符串的方法,他是以行爲單位來進行字符串的處理行爲,正規表示法透過一些特殊符號的輔助,可以讓使用者輕易的達到『搜尋/刪除/取代』某特定字符串的處理程序!
8.2 基礎正規表示法
8.2.1 語系對正規表示法的影響
使用正規表示法時,需要特別留意當時環境的語系爲何,否則可能會發現與別人不相同的擷取結果喔!底下的很多練習都是使用『 』這個語系數據來進行
特殊符號 | 代表意義 |
[:alnum:] | 代表英文大小寫字符及數字,亦即 0-9, A-Z, a-z |
[:alpha:] | 代表任何英文大小寫字符,亦即 A-Z, a-z |
[:blank:] | 代表空格鍵與 [Tab] 按鍵兩者 |
[:cntrl:] | 代表鍵盤上面的控制按鍵,亦即包括 CR, LF, Tab, Del.. 等等 |
[:digit:] | 代表數字而已,亦即 0-9 |
[:graph:] | 除了空格符 (空格鍵與 [Tab] 按鍵) 外的其他所有按鍵 |
[:lower:] | 代表小寫字符,亦即 a-z |
[:print:] | 代表任何可以被打印出來的字符 |
[:punct:] | 代表標點符號 (punctuation symbol),亦即:" ' ? ! ; : # $... |
[:upper:] | 代表大寫字符,亦即 A-Z |
[:space:] | 任何會產生空白的字符,包括空格鍵, [Tab], CR 等等 |
[:xdigit:] | 代表 16 進位的數字類型,因此包括:0-9, A-F, a-f 的數字與字符 |
8.2.2 grep 的一些進階選項
[dmtsai@study ~]$ grep [-A] [-B] [--color=auto] '搜尋字符串' filename
-A :後面可加數字,爲 after 的意思,除了列出該行外,後續的 n 行也列出來;
-B :後面可加數字,爲 befer 的意思,除了列出該行外,前面的 n 行也列出來;
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost tmp]# alias | grep "mv"
[root@localhost tmp]# alias | grep -A1 -B2 "mv"
8.2.3 基礎正規表示法練習
RE 字符 | 意義與範例 |
^word | 意義:待搜尋的字符串(word)在行首! 範例:搜尋行首爲 # 開始的那一行,並列出行號 grep -n '^#' regular_express.txt |
word$ | 意義:待搜尋的字符串(word)在行尾! 範例:將行尾爲 ! 的那一行打印出來,並列出行號 grep -n '!$' regular_express.txt |
. | 意義:代表『一定有一個任意字符』的字符! 範例:搜尋的字符串可以是 (eve) (eae) (eee) (e e),但不能僅有 (ee) !亦即 e 與 e 中間『一定』僅有一個字符,而空格符也是字符! grep -n 'e.e' regular_express.txt |
\ | 意義:跳脫字符,將特殊符號的特殊意義去除! 範例:搜尋含有單引號 ' 的那一行! grep -n \' regular_express.txt |
* | 意義:重複零個到無窮多個的前一個 RE 字符 範例:找出含有 (es) (ess) (esss) 等等的字符串,注意,因爲 * 可以是 0 個,所以 es 也是符合帶搜尋字符串。另外,因爲 * 爲重複『前一個 RE 字符』的符號,因此,在 * 之前必須要緊接着一個 RE 字符喔!例如任意字符則爲『.*』! grep -n 'ess*' regular_express.txt |
[list] | 意義:字符集合的 RE 字符,裏面列出想要擷取的字符! 範例:搜尋含有 (gl) 或 (gd) 的那一行,需要特別留意的是,在 [] 當中『謹代表一個待搜尋的字符』,例如『 a[afl]y 』代表搜尋的字符串可以是 aay, afy, aly 即 [afl] 代表 a 或 f 或 l 的意思! grep -n 'g[ld]' regular_express.txt |
[n1-n2] | 意義:字符集合的 RE 字符,裏面列出想要擷取的字符範圍! 範例:搜尋含有任意數字的那一行!需特別留意,在字符集合 [] 中的減號 - 是有特殊意義的,他代表兩個字符之間的所有連續字符!但這個連續與否與 ASCII 編碼有關,因此,你的編碼需要設定正確(在 bash 當中,需要確定 LANG 與 LANGUAGE 的變量是否正確!) 例如所有大寫字符則爲 [A-Z] grep -n '[A-Z]' regular_express.txt |
[^list] | 意義:字符集合的 RE 字符,裏面列出不要的字符串或範圍! 範例:搜尋的字符串可以是 (oog) (ood) 但不能是 (oot) ,那個 ^ 在 [] 內時,代表的意義是『反向選擇』的意思。例如,我不要大寫字符,則爲 [^A-Z]。但是,需要特別注意的是,如果以 grep -n [^A-Z] regular_express.txt 來搜尋,卻發現該檔案內的所有行都被列出,爲什麼?因爲這個 [^A-Z] 是『非大寫字符』的意思,因爲每一行均有非大寫字符,例如第一行的"Open Source"就有 p,e,n,o.... 等等的小寫字 grep -n 'oo[^t]' regular_express.txt |
\{n,m\} | 意義:連續 n 到 m 個的『前一個 RE 字符』 意義:若爲 \{n\} 則是連續 n 個的前一個 RE 字符, 意義:若是 \{n,\} 則是連續 n 個以上的前一個 RE 字符! 範例:在 g 與 g 之間有 2 個到 3 個的 o 存在的字符串,亦即 (goog)(gooog) grep -n 'go\{2,3\}g' regular_express.txt |
[root@localhost tmp]# cat test
"Open Source" is a good mechanism to develop programs.
Football game is not use feet only.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
the symbol '*' is represented as start.
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
google is the best tools for search keyword.
[root@localhost tmp]# grep -n "the" test #查找含有"the"的行
8:I can't finish the test.^M #-n,顯示行號
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -vn "the" test #-v,查找不含"the"的行
[root@localhost tmp]# grep -in "the" test #-i,不區分大小寫
[root@localhost tmp]# grep -n 't[ae]st' test #查找含有test或者tast的行
[root@localhost tmp]# grep -n 'oo' test #查找含有oo的行
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '[^g]oo' test #不是以g開頭的oo
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '[^a-z]oo' test #不是以小寫字母開頭的oo
3:Football game is not use feet only.
[root@localhost tmp]# grep -n '[^[:lower:]]oo' test
[root@localhost tmp]# grep -n '[0-9]' test #含有數字的行
5:However, this dress is about $ 3183 dollars.^M
15:You are the best is mean you are the no. 1.
[root@localhost tmp]# grep -n '[[:digit:]]' test
[root@localhost tmp]# grep -n '^the' test #以the開頭的行
12:the symbol '*' is represented as start.
[root@localhost tmp]# grep -n '^[0-9]' test #以數字開頭的行
[root@localhost tmp]# grep -n '^[[:digit:]]' test
[root@localhost tmp]# grep -n '^[a-z]' test #以小寫字母開頭的行
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '^[[:lower:]]' test
[root@localhost tmp]# grep -n '^[^a-zA-Z]' test #不是以字母開頭的行
1:"Open Source" is a good mechanism to develop programs.
[root@localhost tmp]# grep -n '\.$' test #以.結尾的行
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '^$' test #空行
[root@localhost tmp]# grep -vn '^$' test | grep -vn '^#' #去掉空行和註釋行
u * (星星號):代表『重複前一個字符, 0 到無窮多次』的意思,爲組合形態
[root@localhost tmp]# grep -n 'g..d' test #g和d中間有兩個字符
1:"Open Source" is a good mechanism to develop programs.
16:The world <Happy> is the same with "glad".
[root@localhost tmp]# grep -n 'ooo*' test #兩個或以上的o
[root@localhost tmp]# grep -n 'goo*g' test #兩個g之間至少有一個o
[root@localhost tmp]# grep -n 'g*g' test #至少一個g
[root@localhost tmp]# grep -n 'g.*g' test #含有兩個g
[root@localhost tmp]# grep -n '[0-9][0-9]*' test #含有數值
[root@localhost tmp]# grep -n '[0-9]' test
[root@localhost tmp]# grep -n 'o\{2\}' test #含有連續兩個o
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'ooo*' test
[root@localhost tmp]# grep -n 'o\{2,5\}' test #含有連續2-5個o
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'o\{5,\}' test #含有5個或以上個o
[root@localhost tmp]# grep -n 'go\{2,\}g' test #g和g之間含有2個或以上個o
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'gooo*g' test
[root@localhost tmp]# grep -n 'go\{2,5\}g' test #g和g之間含有2-5個o
18:google is the best tools for search keyword.