Presentation is loading. Please wait.

Presentation is loading. Please wait.

©Brooks/Cole, 2001 Chapter 9 Regular Expressions ( 정규수식 )

Similar presentations


Presentation on theme: "©Brooks/Cole, 2001 Chapter 9 Regular Expressions ( 정규수식 )"— Presentation transcript:

1 ©Brooks/Cole, 2001 Chapter 9 Regular Expressions ( 정규수식 )

2 ©Brooks/Cole, 2001 A regular expression is a pattern consisting of a sequence of characters that is matched against text. UNIX evaluates text against the pattern to determine if the text and pattern match. If they match, the expression is true and a command is executed

3 ©Brooks/Cole, 2001 Figure 9-1 Regular Expression( 정규수식 )

4 ©Brooks/Cole, 2001 Figure 9-2 Atoms

5 ©Brooks/Cole, 2001 Figure 9-3 Single-Character Pattern Example

6 ©Brooks/Cole, 2001 Figure 9-4 Dot Atom Example

7 ©Brooks/Cole, 2001 Figure 9-5 Class Atom Example

8 ©Brooks/Cole, 2001 Figure 9-6 Example of Classes

9 ©Brooks/Cole, 2001 Examples PatternMatches ^.$^.$ A line with any single character ^[01234]$ Any line that contains exactly one digit [0-9] Any digit [^0-9] Any character other than a digit []0-9] Any digit or a ] [A-Za-z0-9] A single character that is a letter, digit

10 ©Brooks/Cole, 2001 Figure 9-7 Anchors

11 ©Brooks/Cole, 2001 AnchorsThe Anchors Characters: ^ and $ – The ^ is only an anchor if it is the first character in a regular expression. – The $ is only an anchor if it is the last character. – If the anchor characters are not used at the proper end of the pattern, then they no longer act as anchors. – If you need to match anchor characters, you must escape the special character by typing a \ before it.

12 ©Brooks/Cole, 2001 AnchorsThe Anchors Characters: ^ and $ PatternMatches ^A^A An A at the beginning of a line A$A$ An A at the end of a line A An A anywhere on a line $A$A A $A anywhere on a line A^ A A^ anywhere on a line ^^ or ^\^ A ^ at the beginning of a line $$ or \$$ A $ at the end of a line

13 ©Brooks/Cole, 2001 The characters \ The characters \ – They do anchor the expression between to match only if it is on a word boundary. – They don’t occupy a position of a character. – For example, the pattern to search for the words the and The would be \

14 ©Brooks/Cole, 2001 Figure 9-8 Operators

15 ©Brooks/Cole, 2001 Figure 9-9 Example of Sequence Operator

16 ©Brooks/Cole, 2001 Figure 9-10 Evaluation of a String Using Sequence Operator

17 ©Brooks/Cole, 2001 Figure 9-11 Alternation Operator

18 ©Brooks/Cole, 2001 Figure 9-12 Matching Alternation Operators

19 ©Brooks/Cole, 2001 Figure 9-13 Repetition Operator

20 ©Brooks/Cole, 2001 Figure 9-14 Basic Repetition Forms

21 ©Brooks/Cole, 2001 Figure 9-15 Example of Short Form Repetition Operators

22 ©Brooks/Cole, 2001 Figure 9-16 Repeating Pattern Matching

23 ©Brooks/Cole, 2001 Figure 9-17 Greedy Matching

24 ©Brooks/Cole, 2001 Repetition PatternMatches ^A*^A* Any line ^AA* Any line starting with one A ^AA*B Any line starting with one or more A's followed by a B ^A\{4,8\} B Any line starting with four, five, six, seven, or eight A's followed by a B ^A\{4,\}B Any line starting with four or more A's followed by a B ^A\{4\}B Any line starting with an AAAAB

25 ©Brooks/Cole, 2001 Figure 9-18 Group Operator

26 ©Brooks/Cole, 2001 Figure 9-19 Saving

27 ©Brooks/Cole, 2001 Exercise 23. How many character can be matched by each of the following regular expressions? a)[ABC]c) \2 b).d) ABC 24. Simplyfy the following regular expressions. a)[ABCDE]c) [0123456] b)[ABCDEnmopq]d) [ABCDEa-q]

28 ©Brooks/Cole, 2001 25. What is the minimum length of the line to be matched by each of the following regular expressions? a)[ABC][0-9][L-N]c) ^…$ b)^$d) ^.*$ 27. How many characters are matched by the following regular expressions? a)A\{4\}c) A\{,7\} b)A\{4,7\}d) A\{8,\}

29 ©Brooks/Cole, 2001 28. If possible, rewrite the following regular expressions using other operators: a)A\{0, \}  A*c) A\{0, 1\} b)A\{1, \}d) A\{2, 3\} 53. Write a regular expression that matches a blank line. 54. Write a regular expression that matches a nonblank line. 55. Write a regular expression that matches every line.

30 ©Brooks/Cole, 2001 56. Write a regular expression that matches a line with exactly three characters. 57. Write a regular expression that matches a line of at least three characters. 58. Write a regular expression that matches a line of at most three characters. 63. Write a regular expression that matches a digit. 64. Write a regular expression that matches a nondigit character.


Download ppt "©Brooks/Cole, 2001 Chapter 9 Regular Expressions ( 정규수식 )"

Similar presentations


Ads by Google