LinuxRegex的學習使用筆記

來源:互聯網
上載者:User

文章主要是介紹了linux中最基本的一些使用方法,這裡也是站長學習linux正則的一些經驗與測試執行個體了,下面我們一起來看看。

幾個概念:

1.基本Regex   擴充的Regex  (由一般字元+元字元組成)
2.通配                                            (由一般字元+元字元組成)
3.元字元

注意:Regex中元字元的意義和通配中元字元的意義有區別的

bash shell本身不支援Regex,使用Regex的是shell的一些命令和工具,如grep,sed,awk等等
但是bash可以使用Regex中的一些元字元實現通配的功能,此時的這些元字元叫萬用字元。
此時通配中元字元的意義跟Regex中元字元的意義就不一樣了。

通配是指:將一個包含萬用字元的非具體的檔案名稱擴充為電腦,伺服器,網路上的一批具體的檔案名稱的過程。

基本Regex中元字元的意義如下:

例子,我們先把下面檔案儲存成regular_express.txt檔案

"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!

go! go! Let's go.
# I am VBird

2. 首先看看裡面的內容,vim regular_express.txt

 

3. 尋找特定的字串
  使用:q命令退出來,如果不小心對內容作了改動,用:q!強行退出即可
  然後輸入

grep -n ‘the’ regular_express.txt

即可顯示有the的行(PS:Linux對大小寫敏感,曾經把phpMyAdmin的首頁用apache放出來,由於M和A沒有大寫,倒騰了很久才發現網頁打不開是因為這個原因)

查詢結果如下:

4. 反向選擇,就是尋找沒有這個字串的內容(windows沒這功能)

 

grep -vn ‘the’ regular_express.txt

加個v就ok了,也許是reverse

 

5. 忽略大小寫進行尋找

 

grep -in ‘the’ --color=auto regular_express.txt

加個i即可,ignore之意。可以看見,多了第九和第十四行。

 

6. 利用中括弧模糊搜尋

譬如要尋找taste和test兩個單詞,發現他們都是 t■st 格式的,於是可以用命令

grep -n ‘t[ae]st’ regular_express.txt

 

如果只想查有oo字元的,使用如下命令:

 

grep -n ‘oo’ regular_express.txt

 

不想搜到前面有g的,利用[^]來排除

 

grep -n ‘[^g]oo’ regular_express.txt

 

再者,如果不想oo前面有小寫字母,可以這樣

 

grep -n ‘[^a-z]oo’ regular_express.txt

 

7. 希望行首行尾是某個字元(^)

grep -n '^the' regular_express.txt

 

希望開頭不是英文字母

 

grep -n '^[^A-Za-z]' regular_express.txt

看得有點暈?第一個^,意思為必須符合;第二個^,意思為非,不是。於是我們搜尋到的是以符號或數字開頭的行。a-z表示所有小寫字母,A-Z表示所有大寫字母。

 

尋找以‘.’結尾的行

grep -n ‘.$’ regular_express.txt

逸出字元用於.的特殊意義消除。為什麼5-9行沒有print出來?

我們用cat看看

cat -An regular_express.txt

它們的結尾是^M$,這是什嗎?其實這是windows斷行字元和Linux斷行字元$的差別。

 

8. 任意一個字元.和重複字元*

grep -n ‘g..d’ regular_express.txt

一定有2個字元,至於是什麼不用管。

 

*,有0個到無窮個

grep -n 'ooo*' regular_express.txt

前兩個oo表示必須存在的,第三個o表示有0個至無限個。

 

9. 限定連續RE字元範圍{}

grep -n 'go{2,5}g' regular_express.txt

goooooole 終於不被選上了。為什嗎?

因為我們限定了o只能出現2-5次,上面這個單詞的o出現了6次!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.