page counter next up previous contents
Next: 延伸正規表示法 Up: 正規表示法與管線處理 Previous: 正規表示法與管線處理   Contents

基礎正規表示法

  • 什麼是正規表示法
    1. 處理字串的方法,以行為單位進行字串的處理行為。
    2. 透過一些特殊符號的輔助,讓使用者輕易的達到搜尋、取代,限定某特定字串的處理程序。
    3. 延伸的正規表示法:可以作群組的字串『或(or)』及『且(and)』的處理。
  • 正規表示法用途
    1. 系統管理員的可以透過『正規表示法』的功能,將登錄的資訊進行處理,僅取出『有問題』的資訊來進行分析。
    2. 一般支援正規表示法的軟體可利用其進行字串的處理,例如:『郵件伺服器』以『字串』的比對來過濾郵件。
    3. 軟體中輸入欄位的格式限制,例如:只能為數字。
  • less, man, vim, grep, awk ,sed 等工具皆支援正規表示法。
  • 重要特殊字元(characters)
    RE字符         意義與範例
    ^word    待搜尋的字串(word)在行首。
             範例:grep -n '^#' re.txt
             搜尋行首為 # 開始的那一行。
    word$    待搜尋的字串(word)在行尾。
             範例:grep -n '!$' re.txt
             將行尾為 ! 的那一行列印出來。
      .      代表『任意一個』字符,一定是一個任意字符。
             範例:grep -n 'e.e' re.txt
             搜尋的字串可以是 (eve) (eae) (eee) (e e), 但不能僅有 (ee)。
             亦即 e 與 e 中間『一定』僅有一個字元,而空白字元也是字元。
      \      跳脫字符,將特殊符號的特殊意義去除。
             範例:grep -n \' re.txt
             搜尋含有單引號 ' 的那一行。
      *      重複零個或多個的前一個 RE 字符
             範例:grep -n 'ess*' re.txt
             找出含有 (es) (ess) (esss) 等等的字串。
    \{n,m\}  連續 n 到 m 個的『前一個 RE 字符』
             若為 \{n\} 則是連續 n 個的前一個 RE 字符,
             若是 \{n,\} 則是連續 n 個以上的前一個 RE 字符。
             範例:grep -n 'go\{2,3\}g' re.txt
             在 g 與 g 之間有 2 個到 3 個的 o 存在的字串,亦即 (goog)(gooog)
      [ ]    在 [ ] 當中『謹代表一個待搜尋的字元』
             範例:grep -n 'g[ld]' re.txt
             搜尋含有 (gl) 或 (gd) 的那一行
             範例:grep -n '[0-9]' re.txt
             搜尋含有任意數字的那一行。
             在字元集合 [ ] 中的減號 - 是代表兩個字元之間的所有連續字元。
             [^]:^ 在 [ ] 內時, 代表的意義是『反向選擇』
             範例:grep -n 'oo[^t]' re.txt
             搜尋的字串可以是 (oog) (ood) 但不能是 (oot)。 
             [[:digit:]] 數字,與[0-9]相同
             [[:lower:]] 小寫字元,與[a-z]相同
             [[:upper:]] 大寫字元,與[A-Z]相同
             更多的表示法可下指令 man 7 regex 查詢。
    
  • 以 grep 擷取字串
    1. 準備一純文字檔
      [root@kvm5 ~]# cp /etc/auto.master /tmp
      [root@kvm5 ~]# cat /tmp/auto.master 
      # Sample auto.master file
      # This is an automounter map and it has the following format
      # key [ -mount-options-separated-by-comma ] location
      # For details of the format look at autofs(5).
      #
      /misc	/etc/auto.misc
      #
      # NOTE: mounts done from a hosts map will be mounted with the
      #	"nosuid" and "nodev" options unless the "suid" and "dev"
      #	options are explicitly given.
      #
      /net	-hosts
      #
      # Include central master map if it can be found using
      # nsswitch sources.
      #
      # Note that if there are entries for /net or /misc (as
      # above) in the included master map any keys that are the
      # same will not be seen as the first read key seen takes
      # precedence.
      #
      +auto.master
      
    2. 搜尋特定字串:從檔案中取得特定字串
      [root@kvm5 ~]# grep 'format' /tmp/auto.master 
      # This is an automounter map and it has the following format
      # For details of the format look at autofs(5).
      [root@kvm5 ~]# grep --color 'format' /tmp/auto.master 
      # This is an automounter map and it has the following format
      # For details of the format look at autofs(5).
      
    3. 行首為+
      [root@kvm5 ~]# grep --color '^+' /tmp/auto.master 
      +auto.master
      
    4. 行尾為the
      [root@kvm5 ~]# grep --color 'the$' /tmp/auto.master 
      # NOTE: mounts done from a hosts map will be mounted with the
      # above) in the included master map any keys that are the
      
    5. 包含auto.*
      [root@kvm5 ~]# grep --color 'auto.*' /tmp/auto.master 
      # Sample auto.master file
      # This is an automounter map and it has the following format
      # For details of the format look at autofs(5).
      /misc	/etc/auto.misc
      +auto.master
      [root@kvm5 ~]# grep --color 'auto\.*' /tmp/auto.master 
      # Sample auto.master file
      # This is an automounter map and it has the following format
      # For details of the format look at autofs(5).
      /misc	/etc/auto.misc
      +auto.master
      [root@kvm5 ~]# grep --color 'auto\.\.*' /tmp/auto.master 
      # Sample auto.master file
      /misc	/etc/auto.misc
      +auto.master
      
    6. 利用 [ ] 來搜尋集合字元
      [root@kvm5 ~]# grep --color 'auto[a-z]' /tmp/auto.master 
      # This is an automounter map and it has the following format
      # For details of the format look at autofs(5).
      
    7. 利用 [ ] 反向搜尋集合字元
      [root@kvm5 ~]# grep --color 'auto[^a-z]' /tmp/auto.master 
      # Sample auto.master file
      /misc	/etc/auto.misc
      +auto.master
      
    8. 搜尋數字
      [root@kvm5 ~]# grep --color '[0-9]' /tmp/auto.master 
      # For details of the format look at autofs(5).
      
    9. 搜尋大寫字元
      [root@kvm5 ~]# grep --color '[[:upper:]]' /tmp/auto.master 
      # Sample auto.master file
      # This is an automounter map and it has the following format
      # For details of the format look at autofs(5).
      # NOTE: mounts done from a hosts map will be mounted with the
      # Include central master map if it can be found using
      # Note that if there are entries for /net or /misc (as
      
    10. 搜尋非行首非#,即非註解行
      [root@kvm5 ~]# grep --color '^[^#]' /tmp/auto.master 
      /misc	/etc/auto.misc
      /net	-hosts
      +auto.master
      
  • 例題:以正規表示法顯示行號方式找出目錄 /etc 以下符合下列條件之檔案。
    1. 包含 boot 或 root 字串的檔案。
    2. 包含 b 開頭 t 結尾之字串的檔案。
    3. b 開頭 t 結尾且中間 1 個 o 以上之字串的檔案。
    4. 包含 b 開頭 t 結尾且中間 2 至 4 個 o 之字串的檔案。
    5. 包含行首為 root 之字串的檔案。
    6. 包含行尾為 root 之字串的檔案。
  • 例題:想要查出來檔案中含有 ! 與 > 的字行( ! 在正規表示法中並不是特殊字元):
    grep -n '[!>]' re.txt
    



2015-04-13