正則表達式入門

看了一點Ellie Quigley的《UNIX Shell 範例精解》,學了一點正則表達式入門的知識。總結一下:
 
正則表達式是一種字符模式,用於在查找過程中匹配相同的字符。在大多數程序裏,正則表達式都被擴在2個正斜槓“/”之間。
 
(1)正則表達式元字符
---------------------------------------------------------------------------------------------------------
元字符                      功能                                     示例                               解釋
--------------------------------------------------------------------------------------------------------
   ^                     行首定位符                           /^love/                        匹配以love開頭的行
   $                     行尾定位符                           /love$/                        匹配以love結尾的行
                        匹配一個字符                       /l..e/                            匹配包含l,後跟2個任意字符,再跟一個e的行
   *                     匹配0或任意個前一字符  /o*ve/                         匹配包含任意個o後跟ve的行
  [ ]                    匹配集合中任意一個         /[Ll]ove/                      匹配Love或者love
[x-y]                  匹配範圍中任意一個         /[a-z]/                           匹配任意一個小寫字符
 [^]                     匹配不在指定組內字符    /[^a-z]/                         匹配非小寫字母的一個字符
   \                      用於轉義字符                     /love\./                         匹配包含love後跟一個句號的行
(以下是多數使用正則表達式元字符的UNIX程序都支持的元字符):
 \<                      詞首定位符                         /\<love/                       匹配以love開頭的詞的行(vi and grep)
 \>                      詞尾定位符                         /love\>/                       匹配以love結尾的詞的行
(vi and grep)
\(..\)                   匹配標籤                            /\(love\)\1er/                love模式被保存爲標籤1,此後\1就等價於love
x\{m\}               字符x的m次重複出現       a\{3\}  
x\{m,\}              字符x至少出現m次          a\{3,\}
x\{m,n\}            字符x至少出現m次,至多n次 a\{2,8\}  
 
(2)組合正則表達式元字符
元字符可以組合成爲更爲複雜一些的表達式,例如:
2.1 /^[A-Z]..$/
查找的行以大寫字母開頭,後跟2個任意字符,後跟換行符
2.2/^[A-Z][a-z]*3[0-8]/
查找的行以大寫字母開頭,後跟0或任意個小寫字母,後跟數字3,後跟一個0-8之間的一個數字
2.3/[a-z]*\./
查找的行包含0個或者多個小寫字母后跟一個句點
2.4/*[A-Z][a-z][a-z]$/
查找的行以若干空格開頭,後跟一個大寫字母2個小寫字母,後跟換行
2.5/^[A-Za-z]*[^,][A-Za-z]*$/
查找的行以0或多個大小寫字母開頭,後面不是句號,後跟0或多個大小寫字母,然後換行
 
(3)使用舉例
可以找到多個匹配項時,用n
3.1 關於^和[]
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy 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.
~
~
~
/^[Ll]ove/
說明:匹配以love或者Love開頭
 
3.2 關於.
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy 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.
~
~
~
/l.ve/
說明:匹配包含l後跟任意字符+ve的行
 
3.3 關於*
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy 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*ve/
說明:*是與前面字符緊密聯繫的,如果是/*a/則是表示匹配包含任意個空格之後含a的行
 
3.4 關於[^]
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy 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.
~
~
~
/ove[^a-zA-Z0-9]/
說明:空格可以被匹配到,但是換行符不能被匹配到。這裏是找ove後面跟一個非大消息字母及數字的行。
 
3.5 關於$和[]
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy 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.
~
~
~
/lov[a-z]$/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章