a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > regex > 正文

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

2020-01-20 22:03:20
字體:
供稿:網(wǎng)友

前言

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

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

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

正則表達式的類型

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

在Linux中,常用的正則表達式有:

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

- POSIX 擴展正則表達式(BRE)引擎

基本正則表達式的基本使用

環(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)的單詞,需要注意的有正則表達式模式嚴(yán)格區(qū)分大小寫。

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

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

[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

特殊字符

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

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

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

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

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

如果想要使用這些特殊字符作為普通的文本字符,就需要轉(zhuǎn)義(escape)它,即是在該字符前添加一個特殊字符,向正則表達式引擎說明:它應(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.99hello,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.99hello,world!$5.00#$#$This is "/".This is ^ test. [root@service99 regular]# grep --color '^' price.txt //直接使用會顯示所有的內(nèi)容This price is $4.99hello,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 //“.”在正則表達式中也有特殊含義,請屏蔽,具體的請往下看This is "/".[root@service99 regular]# grep --color '/. $' price.txt //由于我在輸入的時候,多加了一個空格,所以各位需要慎重和小心This is ^ test.           //在正則表達式中,空格作為字符計。[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 -l121[root@service99 regular]# grep -vE '^#|^$' /etc/vsftpd/vsftpd.conf  //v表示反選,E表示支持?jǐn)U展正則“|”是擴展正則的符號,往下看,后面有anonymous_enable=YESlocal_enable=YESwrite_enable=YESlocal_umask=022anon_upload_enable=YESanon_mkdir_write_enable=YESanon_other_write_enable=YESanon_umask=022dirmessage_enable=YESxferlog_enable=YESconnect_from_port_20=YESxferlog_std_format=YESlisten=YESpam_service_name=vsftpduserlist_enable=YEStcp_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.99This 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.99hello,world!This is "/".This is ^ test. [root@service99 regular]# grep --color "[sxyz]" price.txt This price is $4.99This is "/".This is ^ test. [root@service99 regular]# grep --color "[abcd]" price.txt This price is $4.99hello,world![root@service99 regular]# grep --color "Th[ais]" price.txt //Th 后的第一個字符在【ais】中匹配的This price is $4.99This is "/".This is ^ test. [root@service99 regular]# grep -i --color "th[ais]" price.txt //-i 表示不區(qū)分大小寫This price is $4.99This 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

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

[root@service99 regular]# echo "Yes/no" | grep "[Yy][Ee]"Yes/no[root@service99 regular]# echo "Yes/no" | grep "[Yy].*[Nn]" //*在正則表達式中的用法,請往下看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.99hello,world!$5.00#$#$This is "/".this is ^ test. catcar[root@service99 regular]# sed -n '/[^t]his/p' price.txt This price is $4.99This is "/".[root@service99 regular]# grep --color "[^t]his" price.txt This price is $4.99This is "/".[root@service99 regular]# grep --color "ca[tr]" price.txt catcar[root@service99 regular]# grep --color "ca[^r]" price.txt cat

使用范圍

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

[root@service99 regular]# cat price.txt This price is $4.99hello,world!$5.00#$#$This is "/".this is ^ test. catcar123455691111806[root@service99 regular]# egrep --color '[a-z]' price.txt This price is $4.99hello,world!This is "/".this is ^ test. catcar[root@service99 regular]# egrep --color '[A-Z]' price.txt This price is $4.99This is "/".[root@service99 regular]# grep --color "[0-9]" price.txt This price is $4.99$5.00123455691111806[root@service99 regular]# sed -n '/^[^a-Z]/p' price.txt $5.00#$#$123455691111806[root@service99 regular]# grep --color "^[^a-Z]" price.txt $5.00#$#$123455691111806[root@service99 regular]# echo $LANG  //在使用 [a-Z]時,注意LANG環(huán)境變量的值,該值若是進行修改的話,要注意修改的值的合法性zh_CN.UTF-8 [root@service99 regular]# LANG=en_US.UTF-8

特殊字符類

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

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

[[:cntrl:]] 控制字符

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

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

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

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

[[:punct:]] 所有標(biāo)點符號

[[:lower:]] 小寫字母

[[:upper:]] 大寫字母

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

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

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

星號

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

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

擴展正則表達式

問號

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

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

加號

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

[root@service99 regular]# egrep --color "9+" price.txt This price is $4.99911[root@service99 regular]# egrep --color "1+" price.txt 123455691111806

使用大括號

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

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

- m,n:該正則表達式出現(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.

正則表達式實例

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

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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 91香蕉视频 | 成年人看的羞羞网站 | 成人在线三级 | 免费午夜视频 | t66y最新地址一地址二69 | 日韩欧美精品在线观看 | 伊人精品影院 | 日韩久久一区二区三区 | 日韩免费福利视频 | 国产成人精品亚洲日本在线观看 | 欧美性区 | 国产精品一区二区三区免费 | 国产一区二区三区免费在线观看 | 久久亚洲视频 | 成人av免费在线 | 精品视频久久 | 国内精品在线视频 | 亚洲女人天堂网 | 国产精品不卡视频 | 日韩精品一区二区三区 | 国产免费av一区二区三区 | 欧美精品欧美激情 | 午夜99 | 久久av网| 国产www在线 | 四虎国产精品成人免费4hu | 色av综合在线 | 一区二区日韩在线观看 | 91在线看视频 | 中文字幕一二三区有限公司 | 成年人福利 | 久久免费精品 | 天天澡天天狠天天天做 | 亚洲色图综合 | 日韩成人av在线 | 日韩欧美一区二区三区免费观看 | 成人亚洲视频在线观看 | 在线一区视频 | 99热最新 | 中文字幕精品一区二区三区精品 | 网站av|