本次主要記錄正則表達式的基本運用,其中包含基本正則表達式以及擴展正則表達式的基本使用方法。
基本概念
正則表達式就是處理字符串的方法,它是以行爲單位來進行字符串的處理行爲,通過一些特殊符號的輔助,可以讓用戶輕易達到查找、刪除、替換某特定字符串的處理程序。
其主要功能是文本查詢和字符串操作,它可以匹配文本的一個字符或字符集合。實際上正則表達式完成了數據的過濾,將不滿足正則表達式定義的數據拒絕掉,剩下與正則表達式匹配的數據。
Grep文本搜索工具
grep(global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它搜索與正則表達式匹配的行,並將結果輸送至標準輸出。
grep文本搜索命令格式:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] REGEX [FILES]
OPTIONS:
-i 忽略大小寫
-v 顯示模式匹配不到的行(反轉查找)
-o 只輸出文件中匹配的部分
-c 輸出匹配字符串的行數
-n 輸出匹配行的行數
-E 使用擴展的正則表達式
-A # 顯示除列出該行外的,後續#行也列出來
-B # 顯示除列出該行外的,前面#行也列出來
-l 列出文件內容符合指定的範本樣式的文件名稱
-L 列出文件內容不符合指定的範本樣式的文件名稱
基本正則表達式構成
正則表達式(Regular Expression)由兩種字符構成,一種是在正則表達式中具體特殊意義的”元字符“,另一種是普通的”文本字符“。
其元字符集合及其應用
1. 查找特定字符串
[root@www ~]# grep -in 'the' /lab/regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
12:the symbol '*' is represented as start.
14:The gd software is a library for drafting programs.
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.
2. 反向查找字符串
[root@www ~]# grep -inv 'the' /lab/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 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh! My god!
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:# I am VBird
22:
[root@www lab]# cat t1 t2 t3
HEllo kitty baby
no no no
hello1 abc
hello hello
hello aaa
del hello
dskfkhello
he
------------------
hello1
hello2
hello3
hello hello
-----------------
-----------------
-----------------
-----------------
yes yes yes
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
[root@www lab]# grep -l “hello” t1 t2 t3
t1
t2
[root@www lab]# grep –L ” hello” t1 t2 t3
t3
“.”可以匹配任意單個字符,無論在字符串頭,還是其他位置。
[root@www /]# ll |grep ...x..x..x
dr-xr-xr-x. 2 root root 4096 8月 18 04:40 bin
dr-xr-xr-x. 5 root root 1024 8月 17 20:01 boot
drwxr-xr-x. 10 root root 4096 8月 17 20:09 cgroup
drwxr-xr-x. 17 root root 3740 9月 6 09:17 dev
drwxr-xr-x. 125 root root 12288 9月 6 09:49 etc
drwxr-xr-x. 4 root root 4096 8月 17 20:34 home
drwxr-xr-x. 3 root root 4096 9月 6 09:56 lab
dr-xr-xr-x. 11 root root 4096 8月 17 19:47 lib
dr-xr-xr-x. 10 root root 12288 8月 18 04:40 lib64
drwxr-xr-x. 2 root root 4096 9月 23 2011 media
drwxr-xr-x. 2 root root 0 9月 6 09:17 misc
drwxr-xr-x. 2 root root 4096 9月 23 2011 mnt
drwxr-xr-x. 2 root root 0 9月 6 09:17 net
drwxr-xr-x. 3 root root 4096 8月 17 19:56 opt
dr-xr-xr-x. 149 root root 0 9月 6 2015 proc
dr-xr-xr-x. 2 root root 12288 8月 18 04:40 sbin
drwxr-xr-x. 7 root root 0 9月 6 2015 selinux
drwxr-xr-x. 2 root root 4096 9月 23 2011 srv
drwxr-xr-x. 13 root root 0 9月 6 2015 sys
drwxr-xr-x. 13 root root 4096 8月 17 19:31 usr
drwxr-xr-x. 22 root root 4096 8月 17 19:55 var
[root@www lab]# grep -in 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
5. 匹配指定範圍內的字符串集
使用[ ]匹配特定字符串或字符串集,可以用逗號將括弧內要匹配的不同字符串分開。
[ ] 匹配指定範圍內的任意單個字符
. 如果[ ]內不是範圍,選其一。如[a,b,c]或[abc]等
[root@www ~]# grep -in 't[abce]st' /lab/regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
. 如果[ ]內用連字符“-”來表示範圍,表明字符串範圍從“-”左邊字符開始,到“-”右邊字符結束。如[a-z][A-Z][0-9]等。
[root@www ~]# grep -in '[a-z][a-j]' /lab/t1 /lab/t2 /lab/t3
/lab/t1:1:HELLO Kitty
/lab/t1:2:HEllo kitty baby
/lab/t1:4:hello1 abc
/lab/t1: 5:hello hello
/lab/t1:6:hello aaa
/lab/t1:7:del hello
/lab/t1:8:dskfkhello
/lab/t1:9:he
/lab/t2:1:hello
/lab/t2:2:hello1
/lab/t2:3:hello2
/lab/t2:4:hello3
/lab/t2:5:hello hello
/lab/t3:2:yes yes yes
. [^]表示除指定[ ]外的其他任意單個字符
[root@www ~]# grep -in '[^g]oo' /lab/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!
[root@www ~]# grep -n '[^[:lower:]]oo' /lab/regular_express.txt
3:Football game is not use feet only.
[root@www ~]# grep -n '[^a-z]oo' /lab/regular_express.txt
3:Football game is not use feet only.
. 利用標準字符集匹配任意單個字符
[:alnum:] 代表英文大小寫字符及數據,即0-9,A-Z,a-z
[:alpha:] 代表任何英文大小寫字符,即A-Z,a-z
[:digit:] 代表數字而已,即[0-9]
[:lower:] 代表小寫字符,即[a-z]
[:upper:] 代表大寫字符,即A-Z
[:blank:] 代表空格鍵與[Tab]按鍵
[:cntrl:] 代表鍵盤上面的控制按鍵,即包括CR, LF, Tab, Del等
[:space:] 任何會產生空白的字符,包括空格鍵[Tab] CR等
[:print:] 代表任何可以被打印出來的字符
[:graph:] 除了空格符(空格鍵與[Tab]按鍵)外的其他所有按鍵
[:punct:] 代表標點符號,即"", '', ?, !, ;, :, #, $
[:xdigit:] 代表十六進制的數字類型,因此包括0-9, A-Z, a-f的數字與字符
[root@www ~]# grep -n '[[:digit:]]' /lab/regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
. 匹配任意英文單詞
[A-Za-z]* 或者 [[:alpha:]]*
[root@www ~]# grep -n '[[:alpha:]]*' /lab/character.txt
1:abxy
2:xay
3:xxxxxxy
4:xaaaaby
5:jaby
6:
7:abc
8:abbbc
9:ac
10:accccb
11:
12:abab
13:xyxy
14:abbaxyxy
6. 匹配字符串中特定字符前面的重複次數
.“*”匹配前一個字符≥0次
[root@www lab]# cat character.txt
abxy
xay
xxxxxxy
xaaaaby
jaby
abc
abbbc
ac
accccb
[root@www lab]# grep -in 'x*y' character.txt
1:abxy
2:xay
3:xxxxxxy
4:xaaaaby
5:jaby
.“.*”表示任意長度的任意字符
[root@www lab]# grep -in 'a.*c' character.txt
7:abc
8:abbbc
9:ac
10:accccb
.“\?”匹配前一個字符0或1次
[root@www lab]# grep -in 'xy\?' character.txt
1:abxy
2:xay
3:xxxxxxy
4:xaaaaby
[root@www lab]# grep -in 'ab\?b' character.txt
1:abxy
4:xaaaaby
5:jaby
7:abc
8:abbbc
.“\+”匹配前一個字符≥1次
[root@www lab]# ls -l |grep -n 'x\+t'
3:-rw-r--r--. 1 root root 51 9月 7 07:17 character.txt
4:-rw-r--r--. 1 root root 650 9月 6 09:55 regular_express.txt
.“\{m\}”匹配前一個字符m次
[root@www lab]# grep -in 'kit\{2\}' t1
1:HELLO Kitty
2:HEllo kitty baby
[root@www lab]# grep -in 'ki[[:alpha:]]\{2\}' t1
1:HELLO Kitty
2:HEllo kitty baby
[root@www lab]# grep -in 'x\{3\}y' character.txt
3:xxxxxxy
.“\{m,n\}”匹配前一個字符至少m次,至多n次
[root@www lab]# grep -in 'ac\{1,5\}' character.txt
9:ac
10:accccb
.“\{m,\}”匹配前一個字符≥m次
[root@www lab]# grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
.“\{,n\}”匹配前一個字符≤n次(包含0次)
[root@www lab]# grep -n 'c\{,2\}' character.txt
1:abxy
2:xay
3:xxxxxxy
4:xaaaaby
5:jaby
6:
7:abc
8:abbbc
9:ac
10:accccb
7. 匹配指定位置的字符串
.“^”只匹配行首指明的字符或字符串序列
[root@www lab]# grep -n '^[[:lower:]]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
開頭不是英文字母以及數字
[root@www lab]# grep -n '^[^[:alnum:]]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
.“$”只匹配行尾指明的字符或字符串序列
[root@www lab]# grep -n 'b$' character.txt
10:accccb
.“\<”只匹配單詞詞首的字符或字符串序列
[root@www lab]# grep -n '\<a[[:lower:]]\{1,2\}' character.txt
1:abxy
7:abc
8:abbbc
9:ac
10:accccb
.“\>”只匹配單詞詞尾的字符或字符串序列
[root@www lab]# grep -n '[[:lower:]]\{1,2\}y\>' character.txt
1:abxy
2:xay
3:xxxxxxy
4:xaaaaby
5:jaby
[root@www lab]# grep -n '^$' regular_express.txt
22:
.“^.$”匹配僅一個字符
[root@www lab]# grep -n '^.$' testing
9:j
10:c
8. 分組引用模式所匹配到的字符
\(\) 通過將多個字符整合到一起以當作一個整體進行處理。
分組的小括號內所匹配的內容,執行後會被保存在內置的變量中,這些變量包括\1, \2, …依次類推。
\1: 表示第一組括號中的內容
\2: 表示第二組括號中的內容 …
[root@www lab]# grep -n '\(r..t\).*\1' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
擴展正則表達式構成
通過grep –E來使用擴展正則表達式。而擴展正則表達式的元字符與基本正則表達式差不多,去除了轉義字符。
\(\)變爲用()表示
\{\}變爲用{}表示
匹配一組可選字符
“()”和“|”通常結合使用,其“|”符號表示多個正則表達式的“或”關係
[root@www lab]# grep -nE '(a|b|c|x)y' character.txt
1:abxy
2:xay
3:xxxxxxy
4:xaaaaby
5:jaby