Linux正則表達式基礎入門+擴展

最近在看正則表達式部分的東西,有些東西記錄下.

  1. “ .”(一個點)符號. 點符號用於匹配除換行符之外的任意一個字符。例如:r.t可以匹配rot、rut
  2. “ * ”符號. *符號用於匹配前一個字符0次或任意多次
  3. “ {n,m}”符號
    3.1 {n} 匹配前面的字符n次 grep 'ro\{2\}t' 匹配root
    3.2 {n,} 匹配前面的字符至少n次以上(含n次) grep 'ro\{0,\}t 匹配以r開頭,t結尾, 包含o 0到多次'
    3.3 {n,m} 匹配前面的字符n到m次.
  4. “ ^”符號, 這個符號用於匹配開頭的字符, “^root”匹配的是以字母root開始的行
  5. “ $”符號, “$”用於匹配尾部, “abc$”代表的是以abc結尾的行
  6. “ []”符號, 用於匹配方括號內出現的任一字符. 單項選擇題的答案,可能是A、B、C、D選項中的任意一種,用正則表達式表示就是[ABCD]。如果遇到比較大範圍的匹配,比如說要匹配任意一個大寫字母,就需要使用“-”號做範圍限定,寫成[A-Z],要匹配所有字母則寫成[A-Za-z]。一定要注意,這裏“-”的作用不是充當一個字符
  7. “ \”符號, 這個符號代表轉義字符,我們可以對很多特殊的字符進行“轉義”,讓它只代表字符本身,因此這裏的寫法就應該是[\ \-].
  8. “ <”符號和“>”符號, 這兩個符號分別用於界定單詞的左邊界和右邊界. “\<hello”用於匹配以“hello”開頭的單詞;而“hello\>”則用於匹配以“hello”結尾的單詞. 還可以使用它們的組合—“\<\>”用於精確匹配一個字符串。所以“\<hello\>”可精確匹配單詞hello
  9. “ \d”符號, 匹配一個數字,等價於[0-9].
  10. “ \b”符號, 匹配單詞的邊界. “\bhello\b”可精確匹配“hello”單詞。
  11. “ \B”符號, 匹配非單詞的邊界. hello\B可以匹配“helloworld”中的“hello”
  12. “ \w”符號, 匹配字母、數字和下劃線,等價於[A-Za-z0-9].
  13. “ \W”符號, 匹配非字母、非數字、非下劃線,等價於[^A-Za-z0-9]。
  14. “ \n”符號, 匹配一個換行符。
  15. “ \r”符號, 匹配一個回車符。
  16. “ \t”符號, 匹配一個製表符。
  17. “ \f”符號, 匹配一個換頁符。
  18. “ \s”符號, 匹配任何空白字符。
  19. “ \S”符號, 匹配任何非空白字符。
    擴展正則表達式.
  20. “?”符號, “?”符號用於匹配前一個字符0次或1次. “ro?t”僅能匹配rot或rt。
  21. “+”符號, “+”符號用於匹配前一個字符1次以上. “ro+t”就可以匹配rot、root等
  22. “|”符號, “|”符號是“或”的意思,即多種可能的羅列,彼此間是一種分支關係.
  23. “()”符號, “()”符號通常需要和“|”符號聯合使用,用於枚舉一系列可替換的字符.
    通配符
  24. “*”符號, 代表0個或多個字符.
  25. “?”符號, 代表的是任意一個字符.
  26. “{}”符號, “{}”可擁有匹配所有括號內包含的以逗號隔開的字符.
  27. “^”符號和“!”符號 , 這兩個符號往往和“[]”一起使用,當出現在“[]”中的時候,代表取反。所以[^A](或[!A])代表不是A。
    示例:
    爲了演示grep命令的用法, 首先創建一個文件RegExp.txt,文件內容如下所示:
[hadoop@bdata01 shell ]$ cat RegExp.txt 
----------TEXT BEGIN-----------

good morning teacher
hello world is a script
gold sunshine looks beautiful
golden time flies
god bless me
what a delicious food
they teast Good
you fell glad
wrong word gooood
wrong word g10d
wrong word g12d
wrong word g13d
www.helloworld.com
www@helloworld@com
Upper case
100% means pure
php have a gd module

-----------TEXT END----------------
#1. 搜索含有good的單詞的行, 注意: grep默認是區分大小寫的,所以這裏只會打印有good的行
[hadoop@bdata01 shell ]$ grep 'good' RegExp.txt 
good morning teacher

#2. 搜索含有good單詞的行,不區分大小寫
[hadoop@bdata01 shell ]$ grep -i 'good' RegExp.txt 
good morning teacher
they teast Good

#3. 統計不含good單詞的行,不區分大小寫
[hadoop@bdata01 shell ]$ grep -ivc 'good' RegExp.txt 
19

#4. 搜索以good開頭的行
[hadoop@bdata01 shell ]$ grep '^good' RegExp.txt 
good morning teacher

#5. 搜索以Good結尾的行
[hadoop@bdata01 shell ]$ grep 'Good$' RegExp.txt 
they teast Good

#6. 搜索空行行數
[hadoop@bdata01 shell ]$ grep -c '^$' RegExp.txt 
2

#7. 搜索包含good 和 Good的行
[hadoop@bdata01 shell ]$ grep '[Gg]ood' RegExp.txt 
good morning teacher
they teast Good

#8. 搜索一個包含ood的行,但不能是good 和 Good,使用反選功能
[hadoop@bdata01 shell ]$ grep '[^Gg]ood' RegExp.txt 
what a delicious food
wrong word gooood

#9. 搜索一個以g開頭,中間兩個任意字符,結尾是d的行
[hadoop@bdata01 shell ]$ grep 'g..d' RegExp.txt 
good morning teacher
gold sunshine looks beautiful
golden time flies
you fell glad
wrong word g10d
wrong word g12d
wrong word g13d

#10. 搜索一個以g或者G開頭,中間兩個任意字符,結尾是d的行
[hadoop@bdata01 shell ]$ grep '[Gg]..d' RegExp.txt 
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word g10d
wrong word g12d
wrong word g13d

#11. 搜索以g或G開頭,中間是1或者o,第三個字符是任意字符,最後以d結尾的行
[hadoop@bdata01 shell ]$ grep '[Gg][1o].d' RegExp.txt 
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
wrong word g10d
wrong word g12d
wrong word g13d

#12. 搜索含有gold的行, 精確匹配
[hadoop@bdata01 shell ]$ grep '\<gold\>' RegExp.txt 
gold sunshine looks beautiful
[hadoop@bdata01 shell ]$ grep '\bgold\b' RegExp.txt 
gold sunshine looks beautiful

#13. 搜索以g開頭,中間包含0到無限個o,以d結尾的行
[hadoop@bdata01 shell ]$ grep go*d RegExp.txt 
good morning teacher
god bless me
wrong word gooood
php have a gd module

#14. 搜索以g開頭,中間包含字符,以d結尾的行
[hadoop@bdata01 shell ]$ grep 'g.*d' RegExp.txt 
good morning teacher
gold sunshine looks beautiful
golden time flies
god bless me
you fell glad
wrong word gooood
wrong word g10d
wrong word g12d
wrong word g13d
php have a gd module

#15. 搜索把o錯寫成數字的行
[hadoop@bdata01 shell ]$ grep 'g1[0-9]d' RegExp.txt 
wrong word g10d
wrong word g12d
wrong word g13d

#16. 搜索包含www.helloworld.com的行
[hadoop@bdata01 shell ]$ grep 'www.helloworld.com' RegExp.txt 
www.helloworld.com
www@helloworld@com
這裏需要對.進行轉義
[hadoop@bdata01 shell ]$ grep 'www\.helloworld\.com' RegExp.txt 
www.helloworld.com

#17. 搜索一個將good拼錯,寫了多個額o的行
#搜兩個o及以上的行
[hadoop@bdata01 shell ]$ grep 'go\{2,\}' RegExp.txt 
good morning teacher
wrong word gooood
#搜有四個o的行
[hadoop@bdata01 shell ]$ grep 'go\{4\}' RegExp.txt 
wrong word gooood

#18.搜索g和d之間至少有一個o的行
[hadoop@bdata01 shell ]$ egrep 'go+d' RegExp.txt 
good morning teacher
god bless me
wrong word gooood

#19. 搜索g和d之間只有0個或者1個o的行
[hadoop@bdata01 shell ]$ egrep 'go?d' RegExp.txt 
god bless me
php have a gd module

#20. 搜索有glad或者 gold的行
[hadoop@bdata01 shell ]$ egrep 'glad|gold' RegExp.txt 
gold sunshine looks beautiful
golden time flies
you fell glad
[hadoop@bdata01 shell ]$ egrep 'g(la|ol)d' RegExp.txt 
gold sunshine looks beautiful
golden time flies
you fell glad

參考《Linux系統命令及shell腳本實踐指南》

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章