Linux-基礎正規表示法

8.正規表示法

本章同步視頻:https://edu.51cto.com/sd/e4874

8.1 正規表示法

       正規表示法就是處理字符串的方法,他是以行爲單位來進行字符串的處理行爲,正規表示法透過一些特殊符號的輔助,可讓使用者輕易的達到『搜尋/刪除/取代』某特定字符串的處理程序!git

8.2 基礎正規表示法

8.2.1 語系對正規表示法的影響

       使用正規表示法時,須要特別留意當時環境的語系爲什麼,不然可能會發現與別人不相同的擷取結果喔!底下的不少練習都是使用『 』這個語系數據來進行express

特殊符號bash

表明意義app

[:alnum:]ide

表明英文大小寫字符及數字,亦即 0-9, A-Z, a-z測試

[:alpha:]this

表明任何英文大小寫字符,亦即 A-Z,   a-zgoogle

[:blank:]編碼

表明空格鍵與   [Tab] 按鍵二者spa

[:cntrl:]

表明鍵盤上面的控制按鍵,亦即包括 CR, LF, Tab, Del.. 等等

[:digit:]

表明數字而已,亦即 0-9

[:graph:]

除了空格符 (空格鍵與   [Tab] 按鍵) 外的其餘全部按鍵

[:lower:]

表明小寫字符,亦即 a-z

[:print:]

表明任何能夠被打印出來的字符

[:punct:]

表明標點符號   (punctuation symbol),亦即:" ' ? ! ; : # $...

[:upper:]

表明大寫字符,亦即 A-Z

[:space:]

任何會產生空白的字符,包括空格鍵, [Tab], CR 等等

[:xdigit:]

表明 16 進位的數字類型,所以包括:0-9,   A-F, a-f 的數字與字符

8.2.2 grep 的一些進階選項

1.grep語法

[dmtsai@study ~]$ grep [-A] [-B] [--color=auto] '搜尋字符串' filename

選項與參數:

-A :後面可加數字,爲 after 的意思,除了列出該行外,後續的 n 行也列出來;

-B :後面可加數字,爲 befer 的意思,除了列出該行外,前面的 n 行也列出來;

--color=auto 可將正確的那個擷取數據列出顏色

2.用法

[root@localhost tmp]# alias

alias cp='cp -i'

alias egrep='egrep --color=auto'

alias fgrep='fgrep --color=auto'

alias grep='grep --color=auto'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@localhost tmp]# alias | grep "mv"

alias mv='mv -i'

[root@localhost tmp]# alias | grep -A1 -B2 "mv"

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i'

alias rm='rm -i'

8.2.3 基礎正規表示法練習

RE 字符

意義與範例

^word

意義:待搜尋的字符串(word)在行首!

範例:搜尋行首爲 # 開始的那一行,並列出行號

grep -n '^#' regular_express.txt

word$

意義:待搜尋的字符串(word)在行尾!

範例:將行尾爲 ! 的那一行打印出來,並列出行號

grep -n '!$' regular_express.txt

.

意義:表明『必定有一個任意字符』的字符!

範例:搜尋的字符串能夠是   (eve) (eae) (eee) (e e),但不能僅有 (ee) !亦即 e e 中間『必定』僅有一個字符,而空格符也是字符!

grep -n 'e.e' regular_express.txt

\

意義:跳脫字符,將特殊符號的特殊意義去除!

範例:搜尋含有單引號 ' 的那一行!

grep -n \' regular_express.txt

*

意義:重複零個到無窮多個的前一個 RE 字符

範例:找出含有 (es)   (ess) (esss) 等等的字符串,注意,由於 * 能夠是 0 個,因此 es 也是符合帶搜尋字符串。另外,由於 * 爲重複『前一個 RE 字符』的符號,所以,在 * 以前必需要緊接着一個 RE 字符喔!例如任意字符則爲『.*』!

grep -n 'ess*' regular_express.txt

[list]

意義:字符集合的 RE 字符,裏面列出想要擷取的字符!

範例:搜尋含有 (gl)   (gd) 的那一行,須要特別留意的是,在 [] 當中『謹表明一個待搜尋的字符』,例如『 a[afl]y 』表明搜尋的字符串能夠是 aay, afy, aly [afl] 表明 a f l 的意思!

grep -n 'g[ld]' regular_express.txt

[n1-n2]

意義:字符集合的 RE 字符,裏面列出想要擷取的字符範圍!

範例:搜尋含有任意數字的那一行!需特別留意,在字符集合 [] 中的減號 - 是有特殊意義的,他表明兩個字符之間的全部連續字符!但這個連續與否與 ASCII 編碼有關,所以,你的編碼須要設定正確( bash 當中,須要肯定 LANG LANGUAGE 的變量是否正確!) 例如全部大寫字符則爲   [A-Z]

grep -n '[A-Z]' regular_express.txt

[^list]

意義:字符集合的 RE 字符,裏面列出不要的字符串或範圍!

範例:搜尋的字符串能夠是   (oog) (ood) 但不能是 (oot) ,那個 ^ [] 內時,表明的意義是『反向選擇』的意思。例如,我不要大寫字符,則爲 [^A-Z]。可是,須要特別注意的是,若是以 grep -n [^A-Z]   regular_express.txt 來搜尋,卻發現該檔案內的全部行都被列出,爲何?由於這個 [^A-Z] 是『非大寫字符』的意思,由於每一行均有非大寫字符,例如第一行的"Open Source"就有 p,e,n,o.... 等等的小寫字

grep -n 'oo[^t]' regular_express.txt

\{n,m\}

意義:連續 n m 個的『前一個 RE 字符』

意義:若爲   \{n\} 則是連續 n 個的前一個 RE 字符,

意義:如果   \{n,\} 則是連續 n 個以上的前一個 RE 字符! 範例:在 g g 之間有 2 個到 3 個的 o 存在的字符串,亦即   (goog)(gooog)

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

1.測試文檔

[root@localhost tmp]# cat test

"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.^M

GNU is free air not free beer.^M

Her hair is very beauty.^M

I can't finish the test.^M

Oh! The soup taste good.^M

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.^M

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.搜尋特定字符串

[root@localhost tmp]# grep -n "the" test    #查找含有"the"的行

8:I can't finish the test.^M                   #-n,顯示行號

12:the symbol '*' is represented as start.

15:You are the best is mean you are the no. 1.

16:The world <Happy> is the same with "glad".

18:google is the best tools for search keyword.

[root@localhost tmp]# grep -vn "the" test     #-v,查找不含"the"的行

[root@localhost tmp]# grep -in "the" test      #-i,不區分大小寫

3.利用中括號 [] 來搜尋集合字符

[root@localhost tmp]#  grep -n 't[ae]st' test      #查找含有test或者tast的行

8:I can't finish the test.^M

9:Oh! The soup taste good.^M

[root@localhost tmp]# grep -n 'oo' test     #查找含有oo的行

1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

3:Football game is not use feet only.

9:Oh! The soup taste good.^M

18:google is the best tools for search keyword.

19:goooooogle yes!

[root@localhost tmp]# grep -n '[^g]oo' test     #不是以g開頭的oo

2:apple is my favorite food.

3:Football game is not use feet only.

18:google is the best tools for search keyword.

19:goooooogle yes!

[root@localhost tmp]# grep -n '[^a-z]oo' test    #不是以小寫字母開頭的oo

3:Football game is not use feet only.

#注:下面的語句效果相同:

[root@localhost tmp]# grep -n '[^[:lower:]]oo' test

[root@localhost tmp]# grep -n '[0-9]' test     #含有數字的行

5:However, this dress is about $ 3183 dollars.^M

15:You are the best is mean you are the no. 1.

#注:下面的語句效果相同:

[root@localhost tmp]# grep -n '[[:digit:]]' test

4.行首與行尾字符 ^ $

[root@localhost tmp]# grep -n '^the' test      #以the開頭的行

12:the symbol '*' is represented as start.

[root@localhost tmp]# grep -n '^[0-9]' test   #以數字開頭的行

#注:下面的命令效果相同:

[root@localhost tmp]# grep -n '^[[:digit:]]' test

[root@localhost tmp]# grep -n '^[a-z]' test   #以小寫字母開頭的行

2:apple is my favorite food.

4:this dress doesn't fit me.

10:motorcycle is cheap than car.

12:the symbol '*' is represented as start.

18:google is the best tools for search keyword.

19:goooooogle yes!

20:go! go! Let's go.

#注:下面的命令效果相同:

[root@localhost tmp]# grep -n '^[[:lower:]]' test

[root@localhost tmp]# grep -n '^[^a-zA-Z]' test     #不是以字母開頭的行

1:"Open Source" is a good mechanism to develop programs.

21:# I am VBird

[root@localhost tmp]# grep -n '\.$' test     #以.結尾的行

1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

3:Football game is not use feet only.

4:this dress doesn't fit me.

10:motorcycle is cheap than car.

11:This window is clear.

12:the symbol '*' is represented as start.

15:You are the best is mean you are the no. 1.

16:The world <Happy> is the same with "glad".

17:I like dog.

18:google is the best tools for search keyword.

20:go! go! Let's go.

[root@localhost tmp]# grep -n '^$' test     #空行

22:

[root@localhost tmp]# grep -vn '^$' test | grep -vn '^#'   #去掉空行和註釋行

5.任意一個字符 . 與重複字符 *

u  . (小數點):表明『必定有一個任意字符』的意思;

u  * (星星號):表明『重複前一個字符, 0 到無窮屢次』的意思,爲組合形態

[root@localhost tmp]# grep -n 'g..d' test    #g和d中間有兩個字符

1:"Open Source" is a good mechanism to develop programs.

9:Oh! The soup taste good.^M

16:The world <Happy> is the same with "glad".

[root@localhost tmp]# grep -n 'ooo*' test    #兩個或以上的o

[root@localhost tmp]# grep -n 'goo*g' test    #兩個g之間至少有一個o

[root@localhost tmp]# grep -n 'g*g' test     #至少一個g

[root@localhost tmp]# grep -n 'g.*g' test    #含有兩個g

[root@localhost tmp]# grep -n '[0-9][0-9]*' test     #含有數值

#注:我的感受和下面的命令效果同樣:

[root@localhost tmp]# grep -n '[0-9]' test

6.限定連續 RE 字符範圍 {}

[root@localhost tmp]# grep -n 'o\{2\}' test   #含有連續兩個o

1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

3:Football game is not use feet only.

9:Oh! The soup taste good.^M

18:google is the best tools for search keyword.

19:goooooogle yes!

#注:下面的命令效果同樣:

[root@localhost tmp]# grep -n 'ooo*' test

[root@localhost tmp]# grep -n 'o\{2,5\}' test   #含有連續2-5個o

1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

3:Football game is not use feet only.

9:Oh! The soup taste good.^M

18:google is the best tools for search keyword.

19:goooooogle yes!

[root@localhost tmp]# grep -n 'o\{5,\}' test      #含有5個或以上個o

19:goooooogle yes!

[root@localhost tmp]# grep -n 'go\{2,\}g' test    #g和g之間含有2個或以上個o

18:google is the best tools for search keyword.

19:goooooogle yes!

#注:下面的語句效果同樣:

[root@localhost tmp]# grep -n 'gooo*g' test

[root@localhost tmp]# grep -n 'go\{2,5\}g' test   #g和g之間含有2-5個o

18:google is the best tools for search keyword.

相關文章
相關標籤/搜索