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

Post on 05-Jan-2016

213 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

©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

©Brooks/Cole, 2001

Figure 9-1

Regular Expression( 정규수식 )

©Brooks/Cole, 2001

Figure 9-2

Atoms

©Brooks/Cole, 2001

Figure 9-3

Single-Character Pattern Example

©Brooks/Cole, 2001

Figure 9-4

Dot Atom Example

©Brooks/Cole, 2001

Figure 9-5

Class Atom Example

©Brooks/Cole, 2001

Figure 9-6

Example of Classes

©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

©Brooks/Cole, 2001

Figure 9-7

Anchors

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

©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

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

©Brooks/Cole, 2001

Figure 9-8

Operators

©Brooks/Cole, 2001

Figure 9-9

Example of Sequence Operator

©Brooks/Cole, 2001

Figure 9-10 Evaluation of a String Using Sequence Operator

©Brooks/Cole, 2001

Figure 9-11

Alternation Operator

©Brooks/Cole, 2001

Figure 9-12

Matching Alternation Operators

©Brooks/Cole, 2001

Figure 9-13

Repetition Operator

©Brooks/Cole, 2001

Figure 9-14

Basic Repetition Forms

©Brooks/Cole, 2001

Figure 9-15 Example of Short Form Repetition Operators

©Brooks/Cole, 2001

Figure 9-16

Repeating Pattern Matching

©Brooks/Cole, 2001

Figure 9-17

Greedy Matching

©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

©Brooks/Cole, 2001

Figure 9-18

Group Operator

©Brooks/Cole, 2001

Figure 9-19 Saving

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

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

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

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

top related