Linux下正則表達(dá)式有什么用

這篇文章主要介紹了Linux下正則表達(dá)式有什么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)通城,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792

前言

正則表達(dá)式應(yīng)用廣泛,在絕大多數(shù)的編程語(yǔ)言都可以完美應(yīng)用,在Linux中,也有著極大的用處。

使用正則表達(dá)式,可以有效的篩選出需要的文本,然后結(jié)合相應(yīng)的支持的工具或語(yǔ)言,完成任務(wù)需求。

在本篇博客中,我們使用grep/egrep來(lái)完成對(duì)正則表達(dá)式的調(diào)用,其實(shí)也可以使用sed等工具,但是sed的使用極大的需要正則表達(dá)式,為了在后面sed篇的書寫,就只能這樣排序了,有需要的朋友可以把這兩篇一起來(lái)看。

正則表達(dá)式的類型

正則表達(dá)式可以使用正則表達(dá)式引擎實(shí)現(xiàn),正則表達(dá)式引擎是解釋正則表達(dá)式模式并使用這些模式匹配文本的基礎(chǔ)軟件。

在Linux中,常用的正則表達(dá)式有:

- POSIX 基本正則表達(dá)式(BRE)引擎

- POSIX 擴(kuò)展正則表達(dá)式(BRE)引擎

基本正則表達(dá)式的基本使用

環(huán)境文本準(zhǔn)備

[root@service99 ~]# mkdir /opt/regular
[root@service99 ~]# cd /opt/regular
[root@service99 regular]# pwd
/opt/regular
[root@service99 regular]# cp /etc/passwd temp_passwd

純文本

純文本可以完全匹配對(duì)應(yīng)的單詞,需要注意的有正則表達(dá)式模式嚴(yán)格區(qū)分大小寫。

//grep --color 主要是可以將匹配到的文本高亮顯示,這樣便于觀察效果
[root@service99 regular]# grep --color "root" temp_passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

在正則表達(dá)式中,不必局限于完整的單詞,所定義的文本出現(xiàn)在數(shù)據(jù)流的任意位置,正則表達(dá)式都將匹配。

[root@service99 regular]# ifconfig eth2 | grep --color "add"
eth2   Link encap:Ethernet HWaddr 54:52:01:01:99:02 
     inet addr:192.168.2.99 Bcast:192.168.2.255 Mask:255.255.255.0
     inet6 addr: fe80::5652:1ff:fe01:9902/64 Scope:Link

當(dāng)然也不必局限于單獨(dú)的單詞,也可以在文本字符串中出現(xiàn)空格和數(shù)字。

[root@service99 regular]# echo "This is line number 1" | grep --color "ber 1"
This is line number 1

特殊字符

在正則表達(dá)式模式中使用文本字符串時(shí),有一個(gè)問(wèn)題需要注意。

在正則表達(dá)式中定義文本字符串時(shí)有幾個(gè)例外,正則表達(dá)式賦予了它們特殊的含義,如果在文本中使用這些特殊字符,有可能得不到預(yù)期的效果。

正則表達(dá)式認(rèn)可的特殊字符:

復(fù)制代碼 代碼如下:


.*[]^${}+?|()

如果想要使用這些特殊字符作為普通的文本字符,就需要轉(zhuǎn)義(escape)它,即是在該字符前添加一個(gè)特殊字符,向正則表達(dá)式引擎說(shuō)明:它應(yīng)該將下一個(gè)字符解釋為普通文本字符。

實(shí)現(xiàn)該功能的特殊字符是:“\”反斜杠字符

[root@service99 regular]# echo "This cat is $4.99" //雙引號(hào)不會(huì)屏蔽特殊符號(hào),所以系統(tǒng)會(huì)讀取變量4.99的值,然而當(dāng)前系統(tǒng)并沒(méi)有該變量,就顯示為空  
This cat is .99
[root@service99 regular]# echo "This cat is \$4.99"  //使用"\"轉(zhuǎn)義$
This cat is $4.99
[root@service99 regular]# echo 'This cat is \$4.99'  //單引號(hào)屏蔽元字符$
This cat is \$4.99
[root@service99 regular]# echo 'This cat is $4.99' 
This cat is $4.99
[root@service99 regular]# cat price.txt 
This price is $4.99
hello,world!
$5.00
#$#$
This is "\".
[root@service99 regular]# grep --color '\\' price.txt 
This is "\".

定位符

從頭開(kāi)始

脫字符(^)尖角號(hào)定義從數(shù)據(jù)流中文本行開(kāi)頭開(kāi)始的模式。

[root@service99 regular]# grep --color '^h' price.txt  //以字母h開(kāi)頭的行
hello,world!
[root@service99 regular]# grep --color '^$' price.txt //無(wú)輸出結(jié)果,由于沒(méi)有屏蔽特殊含義
[root@service99 regular]# grep --color '^\$' price.txt   //以$符號(hào)開(kāi)頭的行
$5.00
[root@service99 regular]# echo "This is ^ test. " >> price.txt 
[root@service99 regular]# cat price.txt 
This price is $4.99
hello,world!
$5.00
#$#$
This is "\".
This is ^ test. 
[root@service99 regular]# grep --color '^' price.txt //直接使用會(huì)顯示所有的內(nèi)容
This price is $4.99
hello,world!
$5.00
#$#$
This is "\".
This is ^ test. 
[root@service99 regular]# grep --color '\^' price.txt //單獨(dú)使用,并在最前面時(shí)需要屏蔽
This is ^ test. 
[root@service99 regular]# grep --color 'is ^' price.txt //符號(hào)不在最前面時(shí),無(wú)需屏蔽,直接使用即可
This is ^ test.

查找結(jié)尾

美元符號(hào)$特殊字符定義結(jié)尾定位,在文本模式之后添加這個(gè)特殊字符表示數(shù)據(jù)行必須以此文本模式結(jié)束。

[root@service99 regular]# grep --color '\.$' price.txt //“.”在正則表達(dá)式中也有特殊含義,請(qǐng)屏蔽,具體的請(qǐng)往下看
This is "\".
[root@service99 regular]# grep --color '\. $' price.txt //由于我在輸入的時(shí)候,多加了一個(gè)空格,所以各位需要慎重和小心
This is ^ test.           //在正則表達(dá)式中,空格作為字符計(jì)。
[root@service99 regular]# grep --color '0$' price.txt 
$5.00
[root@service99 regular]# grep --color '9$' price.txt 
This price is $4.99

聯(lián)合定位

比較常用的就是“^$” 表示空行

結(jié)合“^#”,由于#在Linux代表注釋

輸出該文本的有效配置

[root@service99 regular]# cat -n /etc/vsftpd/vsftpd.conf | wc -l
121
[root@service99 regular]# grep -vE '^#|^$' /etc/vsftpd/vsftpd.conf  //v表示反選,E表示支持?jǐn)U展正則“|”是擴(kuò)展正則的符號(hào),往下看,后面有
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

字符出現(xiàn)范圍

{n,m} //前一個(gè)字符出現(xiàn)了n到m次

{n,} //前一個(gè)字符出現(xiàn)了n次以上

{n} //前一個(gè)字符出現(xiàn)了n次

[root@service99 regular]# grep --color "12345\{0,1\}" price.txt 
1234556
[root@service99 regular]# grep --color "12345\{0,2\}" price.txt 
1234556

點(diǎn)字符

點(diǎn)特殊字符用于匹配除換行符之外的任意單個(gè)字符,但點(diǎn)字符必須匹配一個(gè)字符;如果在圓點(diǎn)位置沒(méi)有字符,那么模式匹配失敗。

[root@service99 regular]# grep --color ".s" price.txt 
This price is $4.99
This is "\".
This is ^ test. 
[root@service99 regular]# grep --color ".or" price.txt 
hello,world!

字符類

字符類可以定義一類字符來(lái)匹配文本模式中的某一位置。如果在字符類中的某一字符在數(shù)據(jù)流中,就和模式匹配。
為定義字符類,需要使用方括號(hào)。應(yīng)該將要包括在該類中的所有字符用方括號(hào)括起來(lái),然后模式中使用整個(gè)字符類,就像任意的其他通配符一樣。

[root@service99 regular]# grep --color "[abcdsxyz]" price.txt 
This price is $4.99
hello,world!
This is "\".
This is ^ test. 
[root@service99 regular]# grep --color "[sxyz]" price.txt 
This price is $4.99
This is "\".
This is ^ test. 
[root@service99 regular]# grep --color "[abcd]" price.txt 
This price is $4.99
hello,world!
[root@service99 regular]# grep --color "Th[ais]" price.txt //Th 后的第一個(gè)字符在【ais】中匹配的
This price is $4.99
This is "\".
This is ^ test. 
[root@service99 regular]# grep -i --color "th[ais]" price.txt //-i 表示不區(qū)分大小寫
This price is $4.99
This is "\".
This is ^ test.

如果不能確定某個(gè)字符的大小寫,就可以使用該模式:

[root@service99 regular]# echo "Yes" | grep --color "[yY]es"  []內(nèi)字符順序沒(méi)有影響
Yes
[root@service99 regular]# echo "yes" | grep --color "[Yy]es"
yes

在單個(gè)表達(dá)式內(nèi)可以使用多個(gè)字符類:

[root@service99 regular]# echo "Yes/no" | grep "[Yy][Ee]"
Yes/no
[root@service99 regular]# echo "Yes/no" | grep "[Yy].*[Nn]" //*在正則表達(dá)式中的用法,請(qǐng)往下看
Yes/no

字符類對(duì)數(shù)字同樣支持:

[root@service99 regular]# echo "My phone number is 123456987" | grep --color "is [1234]"
My phone number is 123456987
[root@service99 regular]# echo "This is Phone1" | grep --color "e[1234]"
This is Phone1
[root@service99 regular]# echo "This is Phone1" | grep --color "[1]"
This is Phone1

字符類還有一種極為常見(jiàn)的用途是解析可能拼錯(cuò)的單詞:

[root@service99 regular]# echo "regular" | grep --color "r[ea]g[ua]l[ao]"
regular

否定字符類

用于查找不在該字符類中的字符,只需在字符類范圍的開(kāi)頭添加脫字符(^).

即使使用否定,字符類仍必須匹配一個(gè)字符。

[root@service99 regular]# cat price.txt 
This price is $4.99
hello,world!
$5.00
#$#$
This is "\".
this is ^ test. 
cat
car
[root@service99 regular]# sed -n '/[^t]his/p' price.txt 
This price is $4.99
This is "\".
[root@service99 regular]# grep --color "[^t]his" price.txt 
This price is $4.99
This is "\".
[root@service99 regular]# grep --color "ca[tr]" price.txt 
cat
car
[root@service99 regular]# grep --color "ca[^r]" price.txt 
cat

使用范圍

當(dāng)你需要匹配的字符很多并且有一定規(guī)律時(shí),可以這樣:

[root@service99 regular]# cat price.txt 
This price is $4.99
hello,world!
$5.00
#$#$
This is "\".
this is ^ test. 
cat
car
1234556
911
11806
[root@service99 regular]# egrep --color '[a-z]' price.txt 
This price is $4.99
hello,world!
This is "\".
this is ^ test. 
cat
car
[root@service99 regular]# egrep --color '[A-Z]' price.txt 
This price is $4.99
This is "\".
[root@service99 regular]# grep --color "[0-9]" price.txt 
This price is $4.99
$5.00
1234556
911
11806

[root@service99 regular]# sed -n '/^[^a-Z]/p' price.txt 
$5.00
#$#$
1234556
911
11806
[root@service99 regular]# grep --color "^[^a-Z]" price.txt 
$5.00
#$#$
1234556
911
11806
[root@service99 regular]# echo $LANG  //在使用 [a-Z]時(shí),注意LANG環(huán)境變量的值,該值若是進(jìn)行修改的話,要注意修改的值的合法性
zh_CN.UTF-8 
[root@service99 regular]# LANG=en_US.UTF-8

特殊字符類

用于匹配特定類型的字符。

[[:blank:]] 空格(space)與定位(tab)字符

[[:cntrl:]] 控制字符

[[:graph:]] 非空格(nonspace)字符

[[:space:]] 所有空白字符

[[:print:]] 可顯示的字符

[[:xdigit:]] 十六進(jìn)制數(shù)字

[[:punct:]] 所有標(biāo)點(diǎn)符號(hào)

[[:lower:]] 小寫字母

[[:upper:]] 大寫字母

[[:alpha:]] 大小寫字母

[[:digit:]] 數(shù)字

[[:alnum:]] 數(shù)字和大小寫字母

星號(hào)

在某個(gè)字符之后加一個(gè)星號(hào)表示該字符在匹配模式的文本中不出現(xiàn)或出現(xiàn)多次

[root@service99 regular]# cat test.info 
goole
go go go
come on
goooooooooo
[root@service99 regular]# grep --color "o*" test.info 
goole
go go go
come on
goooooooooo
[root@service99 regular]# grep --color "go*" test.info 
goole
go go go
goooooooooo
[root@service99 regular]# grep --color "w.*d" price.txt   //經(jīng)常與.一起使用
hello,world!

擴(kuò)展正則表達(dá)式

問(wèn)號(hào)

問(wèn)號(hào)表示前面的字符可以不出現(xiàn)或者出現(xiàn)一次。不匹配重復(fù)出現(xiàn)的字符。

[root@service99 regular]# egrep --color "91?" price.txt 
This price is $4.99
911

加號(hào)

加號(hào)表示前面的字符可以出現(xiàn)一次或者多次,但必須至少出現(xiàn)一次,該字符若是不存在,則模式不匹配。

[root@service99 regular]# egrep --color "9+" price.txt 
This price is $4.99
911
[root@service99 regular]# egrep --color "1+" price.txt 
1234556
911
11806

使用大括號(hào)

使用大括號(hào)指定對(duì)可重復(fù)的正則表達(dá)式的限制,通常稱為間隔。

- m:該正則表達(dá)式正好出現(xiàn)m次

- m,n:該正則表達(dá)式出現(xiàn)最少m次,最多n次

[root@service99 regular]# echo "This is test,test is file." | egrep --color "test{0,1}"
This is test,test is file.
[root@service99 regular]# echo "This is test,test is file." | egrep --color "is{1,2}"
This is test,test is file.

正則表達(dá)式實(shí)例

這里有一個(gè)實(shí)例,對(duì)基本的正則表達(dá)式進(jìn)行了練習(xí)和實(shí)例。
因?yàn)檎齽t表達(dá)式,單看概念或者理論還是比較簡(jiǎn)單的,然而在實(shí)際的使用中,卻不是那么好用,一旦用好了,對(duì)效率的提升絕對(duì)時(shí)可觀的。

1.過(guò)濾下載文件中包含 the 關(guān)鍵字

grep --color "the" regular_express.txt

2.過(guò)濾下載文件中丌包含 the 關(guān)鍵字

grep --color -vn "the" regular_express.txt

3.過(guò)濾下載文件中丌論大小寫 the 關(guān)鍵字

grep --color -in "the" regular_express.txt

4.過(guò)濾 test 或 taste 這兩個(gè)單字

grep --color -En 'test|taste' regular_express.txt 
grep --color -i "t[ae]ste\{0,1\}" 1.txt

5.過(guò)濾有 oo 的字節(jié)

grep --color "oo" regular_express.txt

6.過(guò)濾丌想要 oo 前面有 g 的

grep --color [^g]"oo" regular_express.txt 
grep --color "[^g]oo" regular_express.txt

7.過(guò)濾 oo 前面丌想有小寫字節(jié)

egrep --color "[^a-z]oo" regular_express.txt

8.過(guò)濾有數(shù)字的那一行

egrep --color [0-9] regular_express.txt

9.過(guò)濾以 the 開(kāi)頭的

egrep --color ^the regular_express.txt

10.過(guò)濾以小寫字母開(kāi)頭的

egrep --color ^[a-z] regular_express.txt

11.過(guò)濾開(kāi)頭丌是英文字母

egrep --color ^[^a-Z] regular_express.txt

12.過(guò)濾行尾結(jié)束為小數(shù)點(diǎn).那一行

egrep --color $"\." regular_express.txt

13.過(guò)濾空白行

egrep --color "^$" regular_express.txt

14.過(guò)濾出 g??d 的字串

egrep --color "g..d" regular_express.txt

15.過(guò)濾至少兩個(gè) o 以上的字串

egrep --color "ooo*" regular_express.txt 
egrep --color o\{2,\} regular_express.txt

16.過(guò)濾 g 開(kāi)頭和 g 結(jié)尾但是兩個(gè) g 之間僅存在至少一個(gè) o

egrep --color go\{1,\}g regular_express.txt

17.過(guò)濾任意數(shù)字的行

egrep --color [0-9] regular_express.txt

18.過(guò)濾兩個(gè) o 的字串

egrep --color "oo" regular_express.txt

19.過(guò)濾 g 后面接 2 到 5 個(gè) o,然后在接一個(gè) g 的字串

egrep --color go\{2,5\}g regular_express.txt

20.過(guò)濾 g 后面接 2 個(gè)以上 o 的

egrep --color go\{2,\} regular_express.txt

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Linux下正則表達(dá)式有什么用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

網(wǎng)站欄目:Linux下正則表達(dá)式有什么用
網(wǎng)站地址:http://muchs.cn/article8/gecoop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、外貿(mào)建站App開(kāi)發(fā)、網(wǎng)站策劃品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司