當前位置:
首頁 > 知識 > 17個案例帶你3分鐘搞定Linux正則表達式

17個案例帶你3分鐘搞定Linux正則表達式


作者:ansheng


來源:https://blog.ansheng.me/article/examples-of-linux-regular-expressions


正則表達式是一種字元模式,用於在查找過程中匹配製定的字元。


元字元通常在Linux中分為兩類:


Shell元字元,由Linux Shell進行解析;
正則表達式元字元,由vi/grep/sed/awk等文本處理工具進行解析;
正則表達式一般以文本行進行處理,在進行下面實例之前,先為grep命令設置--color參數:

$

 

alias

 grep=

"grep --color=auto"



這樣每次過濾出來的字元串都會帶色彩了。


在開始之前還需要做一件事情,就是創建一個測試用的re-file文件,內容如下:

$

 cat re-file


I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It"s hard to get back in the
groove.


文件內容摘錄自<<UNIX/SHELL範例精解第四版>>

$

 cat linux.txt


Linux is a good 
god assdxw bcvnbvbjk
greatttttt  wexcvxc
operaaaating  dhfghfvx
gooodfs awrerdxxhkl
gdsystem awxxxx
glad
good


正則表達式元字元






  • 特殊的元字元







  • 擴展的正則表達式



實操


匹配以love開頭的所有行

$

 grep 

"^love"

 re-file


love, how much I adore you. Do you know

匹配love結尾的所有行

$

 grep 

"love$"

 re-file


clover. Did you see them?  I can only hope love.

匹配以l開頭,中間包含兩個字元,結尾是e的所有行

$

 grep 

"l..e"

 re-file


I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It"s hard to get back in the

匹配0個或多個空行,後面是love的字元

$

 grep 

" *love"

 re-file


I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.

匹配love或Love

$

 grep 

"[Ll]ove"

 re-file  

# 對l不區分大小寫


I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.

匹配A-Z的字母,其次是ove

$

 grep 

"[A-Z]ove"

 re-file


Lovers were all around us. It is springtime. Oh

匹配不在A-Z範圍內的任何字元行,所有的小寫字元

$

 grep 

"[^A-Z]"

 re-file


I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It"s hard to get back in the
groove.

匹配love.

$

 grep 

"love."

 re-file


clover. Did you see them?  I can only hope love.

匹配空格

$

 grep 

"^$"

 re-file


匹配任意字元

$

 grep 

".*"

 re-file


I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It"s hard to get back in the
groove.

前面o字元重複2到4次

$

 grep 

"o{2,4}"

 re-file


groove.

重複o字元至少2次

$

 grep 

"o{2,}"

 re-file


groove.

重複0字元最多2次

$

 grep 

"o{,2}"

 re-file


I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It"s hard to get back in the
groove.

重複前一個字元一個或一個以

$

 egrep 

"go+d"

 linux.txt


Linux is a good
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good

#

####0個或者一個字元


ansheng@Ubuntu:/tmp$ egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx

或,查找多個字元串

$

 egrep 

"gd|good"

 linux.txt


Linux is a good
gdsystem awxxxx
good

分組過濾匹配

$

 egrep 

"g(la|oo)d"

 linux.txt


Linux is a good
glad
good




●編號604,輸入編號直達本文



●輸入m獲取文章

目錄

推薦↓↓↓

 



運維


更多推薦

18個技術類微信公眾號


涵蓋:程序人生、演算法與數據結構、黑客技術與網路安全、大數據技術、前端開發、Java、Python、Web開發、安卓開發、iOS開發、C/C++、.NET、Linux、資料庫、運維等。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Linux學習 的精彩文章:

Linux性能檢測常用的10個基本命令
詳細到沒朋友,一文幫你理清Linux 用戶與用戶組關係

TAG:Linux學習 |