濮阳杆衣贸易有限公司

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

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

熱門標(biāo)簽:外呼系統(tǒng)無呼出路由是什么原因 甘肅醫(yī)療外呼系統(tǒng)排名 ai電話機(jī)器人搭建 西藏智能外呼系統(tǒng)代理商 地圖標(biāo)注教學(xué)點 呼叫系統(tǒng)外呼只能兩次 梅縣地圖標(biāo)注 貴港公司如何申請400電話 400電話辦理電話辦理

前言

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

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

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

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

正則表達(dá)式可以使用正則表達(dá)式引擎實現(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

純文本

純文本可以完全匹配對應(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 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)然也不必局限于單獨的單詞,也可以在文本字符串中出現(xiàn)空格和數(shù)字。

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

特殊字符

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

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

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

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

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

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

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

[root@service99 regular]# echo "This cat is $4.99" //雙引號不會屏蔽特殊符號,所以系統(tǒng)會讀取變量4.99的值,然而當(dāng)前系統(tǒng)并沒有該變量,就顯示為空  
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'  //單引號屏蔽元字符$
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 "\".

定位符

從頭開始

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

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

查找結(jié)尾

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

[root@service99 regular]# grep --color '\.$' price.txt //“.”在正則表達(dá)式中也有特殊含義,請屏蔽,具體的請往下看
This is "\".
[root@service99 regular]# grep --color '\. $' price.txt //由于我在輸入的時候,多加了一個空格,所以各位需要慎重和小心
This is ^ test.           //在正則表達(dá)式中,空格作為字符計。
[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ò)展正則的符號,往下看,后面有
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} //前一個字符出現(xiàn)了n到m次

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

{n} //前一個字符出現(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

點字符

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

[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!

字符類

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

[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 后的第一個字符在【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. 

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

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

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

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

字符類對數(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

字符類還有一種極為常見的用途是解析可能拼錯的單詞:

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

否定字符類

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

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

[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ī)律時,可以這樣:

[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]時,注意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)點符號

[[:lower:]] 小寫字母

[[:upper:]] 大寫字母

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

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

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

星號

在某個字符之后加一個星號表示該字符在匹配模式的文本中不出現(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á)式

問號

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

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

加號

加號表示前面的字符可以出現(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

使用大括號

使用大括號指定對可重復(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á)式實例

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

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

grep --color "the" regular_express.txt 

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

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

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

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

4.過濾 test 或 taste 這兩個單字

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

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

grep --color "oo" regular_express.txt 

6.過濾丌想要 oo 前面有 g 的

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

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

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

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

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

9.過濾以 the 開頭的

egrep --color ^the regular_express.txt 

10.過濾以小寫字母開頭的

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

11.過濾開頭丌是英文字母

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

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

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

13.過濾空白行

egrep --color "^$" regular_express.txt 

14.過濾出 g??d 的字串

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

15.過濾至少兩個 o 以上的字串

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

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

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

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

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

18.過濾兩個 o 的字串

egrep --color "oo" regular_express.txt 

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

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

20.過濾 g 后面接 2 個以上 o 的

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

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

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

標(biāo)簽:本溪 湖州 大興安嶺 涼山 常州 泰安 哈密 巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解基于Linux下正則表達(dá)式(基本正則和擴(kuò)展正則命令使用實例)》,本文關(guān)鍵詞  詳解,基于,Linux,下,正則,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。

  • 相關(guān)文章
  • 下面列出與本文章《詳解基于Linux下正則表達(dá)式(基本正則和擴(kuò)展正則命令使用實例)》相關(guān)的同類信息!
  • 本頁收集關(guān)于詳解基于Linux下正則表達(dá)式(基本正則和擴(kuò)展正則命令使用實例)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阿图什市| 金阳县| 龙口市| 通渭县| 哈密市| 泾川县| 泽普县| 中宁县| 小金县| 江阴市| 永靖县| 杨浦区| 荔波县| 东辽县| 灌阳县| 和田市| 邢台市| 虎林市| 甘洛县| 惠来县| 弥勒县| 海丰县| 宜良县| 丰台区| 新宾| 宕昌县| 台北市| 达州市| 漳平市| 惠安县| 玛沁县| 旺苍县| 都昌县| 临泽县| 木兰县| 清涧县| 资溪县| 乐都县| 五峰| 乳山市| 宁乡县|