佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 詳解基于Linux下正則表達(dá)式(基本正則和擴(kuò)展正則命令使用實(shí)例)

詳解基于Linux下正則表達(dá)式(基本正則和擴(kuò)展正則命令使用實(shí)例)

熱門(mén)標(biāo)簽:阿里云 團(tuán)購(gòu)網(wǎng)站 Mysql連接數(shù)設(shè)置 服務(wù)器配置 Linux服務(wù)器 電子圍欄 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng)

前言

正則表達(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篇的書(shū)寫(xiě),就只能這樣排序了,有需要的朋友可以把這兩篇一起來(lái)看。

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

正則表達(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ū)分大小寫(xiě)。

//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 eth1 | grep --color "add"
eth1   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)

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

[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ū)分大小寫(xiě)
This price is $4.99
This is "\".
This is ^ test. 

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

[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è)字符類(lèi):

[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

字符類(lèi)對(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

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

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

否定字符類(lèi)

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

即使使用否定,字符類(lè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)行修改的話(huà),要注意修改的值的合法性
zh_CN.UTF-8 
[root@service99 regular]# LANG=en_US.UTF-8

特殊字符類(lèi)

用于匹配特定類(lèi)型的字符。

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

[[:cntrl:]] 控制字符

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

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

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

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

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

[[:lower:]] 小寫(xiě)字母

[[:upper:]] 大寫(xiě)字母

[[:alpha:]] 大小寫(xiě)字母

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

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

星號(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á)式的限制,通常稱(chēng)為間隔。

- 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ò)濾下載文件中丌論大小寫(xiě) 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 前面丌想有小寫(xiě)字節(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ò)濾以小寫(xiě)字母開(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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • linux中mount/umount命令的基本用法及開(kāi)機(jī)自動(dòng)掛載方法
  • Linux下Shell腳本中幾種基本命令的替換區(qū)別
  • 【Linux】linux常用基本命令總結(jié)(推薦)
  • linux下動(dòng)態(tài)網(wǎng)站維護(hù)基本命令小結(jié)
  • linux中嘆號(hào)命令(!)的使用小結(jié)

標(biāo)簽:大理 廣元 萍鄉(xiāng) 江蘇 衢州 棗莊 衡水 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解基于Linux下正則表達(dá)式(基本正則和擴(kuò)展正則命令使用實(shí)例)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    依兰县| 宁强县| 永和县| 长汀县| 永顺县| 固始县| 诏安县| 墨江| 镇平县| 托克托县| 阜平县| 工布江达县| 迭部县| 泰州市| 贺州市| 吐鲁番市| 茂名市| 萨嘎县| 镇宁| 聊城市| 新闻| 离岛区| 平山县| 涿鹿县| 三河市| 乡宁县| 太仓市| 安吉县| 绥江县| 枣庄市| 靖江市| 道真| 安陆市| 临江市| 蓬溪县| 霍山县| 吉首市| 乌兰察布市| 花莲县| 罗源县| 望城县|