©brooks/cole, 2001 chapter 9 regular expressions ( 정규수식 )

30
©Brooks/Cole, 2001 Chapter 9 Regular Expressions ( 정정정정 )

Upload: lily-pearson

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

©Brooks/Cole, 2001

Chapter 9

Regular Expressions( 정규수식 )

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

©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

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

©Brooks/Cole, 2001

Figure 9-1

Regular Expression( 정규수식 )

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

©Brooks/Cole, 2001

Figure 9-2

Atoms

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

©Brooks/Cole, 2001

Figure 9-3

Single-Character Pattern Example

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

©Brooks/Cole, 2001

Figure 9-4

Dot Atom Example

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

©Brooks/Cole, 2001

Figure 9-5

Class Atom Example

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

©Brooks/Cole, 2001

Figure 9-6

Example of Classes

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

©Brooks/Cole, 2001

• Examples

Pattern Matches

^.$ 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

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

©Brooks/Cole, 2001

Figure 9-7

Anchors

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

©Brooks/Cole, 2001

•The AnchorsAnchors 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.

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

©Brooks/Cole, 2001

• The AnchorsAnchors Characters: ^ and $

Pattern Matches

^A An A at the beginning of a line

A$ An A at the end of a line

A An A anywhere on a line

$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

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

©Brooks/Cole, 2001

•The characters The characters \<\< and and \>\>–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 \

<[tT]he\>

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

©Brooks/Cole, 2001

Figure 9-8

Operators

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

©Brooks/Cole, 2001

Figure 9-9

Example of Sequence Operator

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

©Brooks/Cole, 2001

Figure 9-10 Evaluation of a String Using Sequence Operator

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

©Brooks/Cole, 2001

Figure 9-11

Alternation Operator

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

©Brooks/Cole, 2001

Figure 9-12

Matching Alternation Operators

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

©Brooks/Cole, 2001

Figure 9-13

Repetition Operator

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

©Brooks/Cole, 2001

Figure 9-14

Basic Repetition Forms

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

©Brooks/Cole, 2001

Figure 9-15 Example of Short Form Repetition Operators

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

©Brooks/Cole, 2001

Figure 9-16

Repeating Pattern Matching

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

©Brooks/Cole, 2001

Figure 9-17

Greedy Matching

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

©Brooks/Cole, 2001

•Repetition

Pattern Matches

^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

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

©Brooks/Cole, 2001

Figure 9-18

Group Operator

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

©Brooks/Cole, 2001

Figure 9-19 Saving

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

©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]

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

©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,\}

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

©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.

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

©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.