溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Linux中使用正則表達(dá)式

發(fā)布時(shí)間:2021-06-07 17:54:40 來源:億速云 閱讀:142 作者:Leah 欄目:互聯(lián)網(wǎng)科技

本篇文章為大家展示了如何在Linux中使用正則表達(dá)式,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

1、組成

普通字符:普通字符串,沒有特殊含義
特殊字符:在正則表達(dá)式中具有特殊的含義
正則表達(dá)式中常見的meta字符【特殊字符】

2、POSIX BRE【基本】與ERE【擴(kuò)展】中都有的meta字符

\ :通常用于打開或關(guān)閉后續(xù)字符的特殊含義,如(...)【\是轉(zhuǎn)義字符,去掉符號的特殊意義,()、{}等在shell中都有特殊的意義】
.和以及.的區(qū)別:

[root@localhost ~]# cat -n test.txt
     1  gd
     2  god
     3
     4  good
     5  goood
     6  goad
     7
     8  gboad

2.1、. :匹配任意單個(gè)字符(除null,即不能為空)

[root@localhost ~]# grep -n "." test.txt      
1:gd
2:god
4:good
5:goood
6:goad
8:gboad
[root@localhost ~]# grep -n "go.d" test.txt
4:good
6:goad

2.2、 :匹配其前字符任意次,如o,可以是沒有o或者一個(gè)o,也可以是多個(gè)o

[root@localhost ~]# grep -n "*" test.txt
[root@localhost ~]# grep -n "o*" test.txt
1:gd
2:god
3:
4:good
5:goood
6:goad
7:
8:gboad
[root@localhost ~]# echo "gbad" >>test.txt
[root@localhost ~]# echo "pbad" >>test.txt
[root@localhost ~]# echo "kgbad" >>test.txt
[root@localhost ~]# echo "poad" >>test.txt  
[root@localhost ~]# grep -n "go*" test.txt 【o可以沒有,o前面的g一定要匹配】
1:gd
2:god
4:good
5:goood
6:goad
8:gboad
9:gbad
11:kgbad

*2.3、. :匹配任意字符(匹配所有),可以為空**

[root@localhost ~]# grep -n ".*" test.txt
1:gd
2:god
3:
4:good
5:goood
6:goad
7:
8:gboad
9:gbad
10:pbad
11:kgbad
12:poad
[root@localhost ~]# grep -n "go.*" test.txt
2:god
4:good
5:goood
6:goad
[root@localhost ~]# grep -n "po.*" test.txt 
12:poad
[root@localhost ~]# echo "pgoad" >>test.txt   
[root@localhost ~]# grep -n "go.*" test.txt  【匹配go后存在任意字符,可為空】
2:god
4:good
5:goood
6:goad
13:pgoad
[root@localhost ~]#
[root@localhost ~]# grep -n "o.*" test.txt 
2:god
4:good
5:goood
6:goad
8:gboad
12:poad

2.4、^ :匹配緊接著后面的正則表達(dá)式,以...為開頭

[root@localhost tmp]# grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost tmp]#

2.5、$ :匹配緊接著前面的正則表達(dá)式,以...結(jié)尾

[root@localhost tmp]# grep "bash$" /etc/passwd | head -1
root:x:0:0:root:/root:/bin/bash
[root@localhost tmp]#
^$:表示是空行的意思
“#|^$”:匹配以#號開頭的注釋行和空行

2.6、[] :匹配方括號里的任一字符

(如[sS],匹配s或匹配S),其中可用連字符(-)指定連字符的范圍(如[(0-9)],匹配0-9任一字符);[^0-9]如果^符號出現(xiàn)在方括號的第一個(gè)位置,則表示匹配不在列表中的任一字符。

[root@localhost tmp]# cat hosts
192.168.200.1
192.168.200.3
a.b.123.5
23.c.56.1
1456.1.2.4
12.4.5.6.8
[root@localhost tmp]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' hosts  
192.168.200.1
192.168.200.3
1456.1.2.4
12.4.5.6.8
[root@localhost tmp]# grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$' hosts
192.168.200.1
192.168.200.3
[root@localhost tmp]#

2.7、? :匹配前面字符的零次或多次

[root@localhost ~]# grep -E "go?d" test.txt  
gd
god
[root@localhost ~]#
[root@localhost tmp]# cat test
do
does
doxy
[root@localhost tmp]# grep -E "do(es)?" test
do
does
doxy
[root@localhost tmp]#

3、POSIX BRE(基本正則)中才有的字符

{n,m} :區(qū)間表達(dá)式,匹配在它前面的單個(gè)字符重現(xiàn)【重復(fù),緊接著的單個(gè)字符如https{0,1},即重復(fù)s 0-1次。{n}指匹配n次;{n,m}指匹配n至m次,{n,}指匹配至少n次,{,m}匹配至多m次?!綷轉(zhuǎn)義字符】

4、POSIX ERE(擴(kuò)展正則)中才有的字符

4.1、{n,m} :與BRE的{n,m}功能相同

[root@localhost tmp]# grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$' hosts
192.168.200.1
192.168.200.3

4.2、+ :匹配前面正則表達(dá)式的一次或多次

[root@localhost ~]# egrep "go+d" test.txt
god
good
goood
[root@localhost ~]#

4.3、| :表示匹配多個(gè)字符串【或的關(guān)系】

[root@localhost ~]# grep -E "3306|1521" /etc/services
mysql           3306/tcp                        # MySQL
mysql           3306/udp                        # MySQL
ncube-lm        1521/tcp                # nCube License Manager
ncube-lm        1521/udp                # nCube License Manager
[root@localhost ~]#

4.4、( ) :分組過濾,后向引用

分組過濾   

[root@localhost ~]# echo "glad" >> test.txt
[root@localhost ~]# egrep "(la|oo)" test.txt
good
goood
glad

()后向引用;當(dāng)前面匹配部分用小括號的時(shí)候,第一個(gè)括號的內(nèi)容可以在后面部分用\1輸出;以此類推。

 [root@localhost tmp]# ifconfig |sed -rn 's#.*addr:(.*)(B.*)$#\1#gp'
192.168.4.27 

5、正則表達(dá)式的元字符

5.1、\b :匹配一個(gè)單詞邊界

[root@localhost tmp]# cat test       
do
does
doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]# grep "\bdo" test       
do
does
doxy
[root@localhost tmp]# grep "\bdoes" test         
does
[root@localhost tmp]# grep "\bdo\b" test 
do
[root@localhost tmp]#

5.2、\B :匹配非單詞邊界,與\b相反

[root@localhost tmp]# grep "do\B" test   
does
doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]#

5.3、\d :匹配一個(gè)數(shù)字字符,等價(jià)于[0-9]

5.4、\D :匹配一個(gè)非數(shù)字字符,等價(jià)于[^0-9]

5.5、\w :匹配字母、數(shù)字、下劃線,等價(jià)于[A-Za-z0-9_]

還有很多元字符,這里就不一一羅列出來

案例:開機(jī)精簡

[root@localhost ~]# chkconfig --list| egrep -v "crond|network|rsyslog|sshd|sysstat" | awk '{print "chkconfig",$1,"off"}'|bash

上述內(nèi)容就是如何在Linux中使用正則表達(dá)式,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI