天天看點

Linux 正規表達式使用簡介

正規表達式基本介紹

正規表達式使用單個字元串來描述、比對一系列符合某個句法規則的字元串。在很多文本編輯裡,正規表達式通常被用來檢索、替換那些符合某個模式的文本。

正規表達式的基本元素包括普通字元和元字元,例如,a、b、1、2 等字元屬于普通字元,普通字元可以按照字面意思了解,如:a 隻能了解為英文的小寫字母a,沒有其他隐藏含義。而*、^、[]等元字元,Shell賦予了它們超越字面意思的意義,如:*符号的字面意義隻是一個符号,而實際上卻表示了重複前面的字元0 次或多次的隐藏含義。是以,掌握正規表達式基本元素主要是對正規表達式中元字元意義的掌握。POSIX 标準将正規表達式分為兩類:基本的正規表達式和擴充的正規表達式。

2.grep 概念

grep的全稱是globalsearch regular expression(RE)andprint out the line,翻譯過來就是全面搜尋正規表達式并把行列印出來。其實grep并不是一個單獨的程式,而是一個家族。包括grep、egrep和fgrep。egrep和fgrep在使用上與grep并沒有顯著的不同。egrep是grep的擴充,擴充的正規表達式。

3.grep的使用格式

grep [OPTIONS] PATTERN [FILE...]

常用的[OPTIONS]有以下幾個:

-i:比對時忽略大小寫

–color:将比對的字元串加以顔色顯示

-v:顯示未被模式比對到的行

-o:隻顯示被模式比對到的字元串

-A#: 表示在比對的行後将其下面的#行也顯示出來

-B#: 表示在比對的行後将其前面的#行也顯示出來

-C#: 表示在比對的行後将其前後的#行也顯示出來

-E:使用擴充正規表達式

基本正規表達式:

以下執行個體中使用的文本regular_express.txt内容是鳥哥網站上複制過來的。其内容如下。

“Open Source” is a good mechanismto 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! Mygod!

The gd software is a library for draftingprograms.

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

使用的grep和egrep指令顯示的顔色是在使用者的~/.bashrc目錄下定義的别名

alias grep=’grep –color’

alias egrep=’egrep –color’

元字元:

.:比對任意單個字元

例如:

[root@localhostdata]# grep  ‘g..g’ regular_express.txt

google is the best tools for search keyword.

*:表示比對其前面的字元任意次

[root@localhostdata]# grep ‘goo*g’ regular_express.txt

goooooogle yes!

.*:任意長度的任意字元

[root@localhostdata]# grep ‘goo.*g’ regular_express.txt

“OpenSource” is a good mechanism to develop programs.

google is thebest tools for search keyword.

\?:比對其前面的字元1次或0次

[root@localhostdata]# grep ‘gooo\?g’ regular_express.txt

google is thebest tools for search keyword.

\+:其前面的字元至少出現一次

[root@localhostdata]# grep ‘goo\+’ regular_express.txt

Oh!The soup taste good.

\{m\}:精确比對其前面的字元為m次

[root@localhostdata]# grep ‘goo\{2\}’ regular_express.txt

goooooogleyes!

\{m,n\}:比對其前面的字元至少m次,至多n次

[root@localhostdata]# grep ‘goo\{2,5\}’ regular_express.txt

[]:指定比對範圍内的任意單個字元

[root@localhostdata]# grep ‘[HEF]‘ regular_express.txt

Footballgame is not use feet only.

However,this dress is about $ 3183 dollars.

Her hair isvery beauty.

Theworld <Happy>is the same with “glad”.

[^]:指定比對範圍外的任意單個字元

例如:

[root@localhostdata]# tail -2 regular_express.txt|grep ‘[^A-Za-z]‘

# I amVBird

以下是一些常用的特殊字元,就不一一舉例了

[[:space:]]:其中[:space:]表示空白字元這個範圍,再加上一個[]表示比對空白字元

[[:punct:]]:标點符号

[[:lower:]]:小寫字母

[[:upper:]]:大寫字母

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

[[:digit:]]:數字

[[:alnum:]]:數字和大小寫字母

針對位置錨定:

^:錨定行首,表示^後面的任意内容必須出現在行首

[root@localhostdata]# grep ‘^go’ regular_express.txt

google isthe best tools for search keyword.

go! go!Let’s go.

$:錨定行尾,表示$前面的任意内容必須出現在行尾

^$:表示空白行

[root@localhostdata]# grep ‘^$’ regular_express.txt |wc -l

\<或\b:錨定詞首,其後面的任意字元必須作為單詞首部出現

\>或\b:錨定詞尾,其前面的任意字元必須作為單詞尾部出現

[root@localhostdata]# grep ‘^\<root\>’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhostdata]# grep ‘^\broot\b’ /etc/passwd

分組:

\(\):例如\(ab\)*表示比對ab可以出現任意次,代表比對ab這個組合,主要後向引用)

[root@localhostdata]# grep “\(ab\)\{2,3\}” ./fith.txt

abababx

ababababxy

xyababaxbababy

後向引用:模式中,如果使用\(\)實作了分組,在某行文本的檢查中,如果\(\)的模式比對到某内容,此内容後面的模式可以被引用

\1:表示第1個小括号中出現的内容

\2:表示第2個小括号中出現的内容

….

[root@localhostdata]# cat  test

helove his lover

shelike her lover

helike is liker

shelove her liker

helike him

[root@localhostdata]# grep ‘\(l..e\).*\1′ test

此處小括号是(l..e),\1就表示前面小括号出現的是love,\1就代表是love,小括号

中是like,\1就代表是like

擴充正規表達式:

擴充正規表達式,大部分元字元等與基本的正規表達式都相同,一些特殊的如下:

?:不需要使用\,比對其前面的字元1次或0次

[root@localhostdata]# egrep ‘goooo?’ regular_express.txt

+:其前字元至少一次,相當于基本正規表達式的\{1,\}

[root@localhostdata]# egrep ‘goo+’ regular_express.txt

{m,n}:不需要使用\,比對其前面的字元至少m次,至多n次

[root@localhostdata]# egrep ‘go{4,6}’ regular_express.txt

():分組

[root@localhostdata]# egrep ‘(oo){3,5}’ regular_express.txt

|:或者 如a|b,a或者b,是整個左側或右側

ab|cd:代表ab或cd

a(b|c)d

fgrep:不支援正規表達式:

後面跟的任意模式都幫他當作字元去比對,其速度較快,如果不需要正規表達式搜尋,則可以使用它

[root@localhostdata]# fgrep –color ‘gooo’ regular_express.txt

以上簡單的列出了正規表達式的一些使用方法,要熟練掌握,還需要多加練習

繼續閱讀