Presentation is loading. Please wait.

Presentation is loading. Please wait.

NCNU Linux User Group 2010 NCNU Linux User Group 2010 王惟綸 (Wei-Lun Wang) 2010/07/07.

Similar presentations


Presentation on theme: "NCNU Linux User Group 2010 NCNU Linux User Group 2010 王惟綸 (Wei-Lun Wang) 2010/07/07."— Presentation transcript:

1 NCNU Linux User Group 2010 NCNU Linux User Group 2010 王惟綸 (Wei-Lun Wang) 2010/07/07

2 2010/7/7 2 Outline What ’ s a Regular Expression? What ’ s a Regular Expression? The Purpose The Purpose What ’ s grep? What ’ s grep? Various Operators Various Operators Extended Regular Expressions Extended Regular Expressions Exercises Exercises References References

3 2010/7/7 3 What ’ s a Regular Expression? A regular expression is a pattern that describes a set of strings. A regular expression is a pattern that describes a set of strings. Examples X[2-7] = {X2, X3, X4, X5, X6, X7} T[ae]ste? = {Taste, Tast, Teste, Test} Examples X[2-7] = {X2, X3, X4, X5, X6, X7} T[ae]ste? = {Taste, Tast, Teste, Test}

4 2010/7/7 4 The Purpose The regular expression is used to process strings. It makes users easily do searching, replacement, and deletion though the aid of special characters. The regular expression is used to process strings. It makes users easily do searching, replacement, and deletion though the aid of special characters. T[ae]ste? = {Taste, Tast, Teste, Test} -- These four strings, Taste, Tast, Teste, and Test, can be found out by only searching the pattern “T[ae]ste?”. T[ae]ste? = {Taste, Tast, Teste, Test} -- These four strings, Taste, Tast, Teste, and Test, can be found out by only searching the pattern “T[ae]ste?”.

5 2010/7/7 5 What ’ s grep? global regular expression print global regular expression print The grep command searches for the pattern specified by the Pattern parameter and writes each matching line to standard output. [-i ] : ignore the type of upper and lower cases [-v] : inverse the output The grep command searches for the pattern specified by the Pattern parameter and writes each matching line to standard output. [-i ] : ignore the type of upper and lower cases [-v] : inverse the output

6 2010/7/7 6

7 7 alias & unalias

8 2010/7/7 8 Various Operators 1. [ ] presents any one character among those characters inside. 2. [ - ] presents any one character among the code range. 3. [^ ] represents the characters not in the range of a list. 4. ^ Matches the empty string at the beginning of a line. 5. $ Matches the empty string at the end of a line. 6.. Matches any single character. 7. * The preceding item will be matched zero or more times.

9 2010/7/7 9 1. [ ] presents any one character among those characters inside. th[ei] = {the, thi}

10 2010/7/7 10 2. [ - ] presents any one character among the code range. LANG=C : 0 1 2 3 4... A B C D... Z a b c d...z LANG=zh_TW.Big5 : 0 1 2 3 4... a A b B c C d D... z Z

11 2010/7/7 11 LANG=C : 0 1 2 3 4... A B C D... Z a b c d...z LANG=zh_TW.Big5 : 0 1 2 3 4... a A b B c C d D... z Z 2. [ - ] presents any one character among the code range.

12 2010/7/7 12 SymbolMeaning [:alnum:] 代表英文大小寫字元及數字,亦即 0-9, A-Z, a-z [:alpha:] 代表任何英文大小寫字元,亦即 A-Z, a-z [:blank:] 代表空白鍵與 [Tab] 按鍵兩者 [: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 的數字與字元 2. [ - ] presents any one character among the code range.

13 2010/7/7 13 3. [^ ] represents the characters not in the range of a list.

14 2010/7/7 14 4. ^ Matches the empty string at the beginning of a line.

15 2010/7/7 15 5. $ Matches the empty string at the end of a line.

16 2010/7/7 16 6.. Matches any single character.

17 2010/7/7 17 7. * The preceding item will be matched zero or more times. go* = {g, go, goo, gooo, … } goo* = {go, goo, gooo, … }

18 2010/7/7 18 Extended Regular Expressions In basic regular expressions the metacharacters "?", "+", "{", "|", "(", and ")" lose their special meaning; instead use the backslashed versions "\?", "\+", "\{", "\|", "\(", and "\)". In basic regular expressions the metacharacters "?", "+", "{", "|", "(", and ")" lose their special meaning; instead use the backslashed versions "\?", "\+", "\{", "\|", "\(", and "\)". Using grep -E or egrep instead of grep. Using grep -E or egrep instead of grep. 1. + The preceding item will be matched one or more times. 2. ? The preceding item will be matched zero or one time. 3. | represents the preceding item or the following item. 4. ( ) represents group strings. 5. {N} The preceding item is matched exactly N times. 6. {N, } The preceding item is matched N or more times. 7. {N,M} The preceding item is matched at least N times, but not more than M times.

19 2010/7/7 19 1. + The preceding item will be matched one or more times. goo+ = {goo, gooo, goooo, … }

20 2010/7/7 20 2. ? The preceding item will be matched zero or one time. goog? = {goog, goo}

21 2010/7/7 21 3. | represents the preceding item or the following item. goo|fav = {goo, fav}

22 2010/7/7 22 4. ( ) represents group strings. f(oo|ee)d = {food, feed}

23 2010/7/7 23 5. {N} The preceding item is matched exactly N times. go\{2\} = {goo} go\{5\} = {gooooo}

24 2010/7/7 24 6. {N, } The preceding item is matched N or more times.

25 2010/7/7 25 7. {N,M} The preceding item is matched at least N times, but not more than M times. but not more than M times. go\{2,5\}g = {goog, gooog, goooog, gooooog}

26 2010/7/7 26 Exercises 1. What does grep -n '^[^A-z] ' mean? 2. How to find out empty lines? 3. How to find out “ [LUG2010] ” ? 4. Find all files and their contents containing the symbol “ * ” under /etc

27 2010/7/7 27 References http://linux.vbird.org/linux_basic/0330regularex.php http://linux.vbird.org/linux_basic/0330regularex.php http://linux.vbird.org/linux_basic/0330regularex.php http://tldp.org/LDP/Bash-Beginners- Guide/html/chap_04.html http://tldp.org/LDP/Bash-Beginners- Guide/html/chap_04.html http://tldp.org/LDP/Bash-Beginners- Guide/html/chap_04.html http://tldp.org/LDP/Bash-Beginners- Guide/html/chap_04.html http://en.wikipedia.org/wiki/Regular_expression http://en.wikipedia.org/wiki/Regular_expression http://en.wikipedia.org/wiki/Regular_expression http://www.regular-expressions.info/posix.html http://www.regular-expressions.info/posix.html http://www.regular-expressions.info/posix.html


Download ppt "NCNU Linux User Group 2010 NCNU Linux User Group 2010 王惟綸 (Wei-Lun Wang) 2010/07/07."

Similar presentations


Ads by Google