Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen.

Similar presentations


Presentation on theme: "Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen."— Presentation transcript:

1 Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen

2 Introduction n Review and Overviews n Chapters 7 and 8 n Summary n Lab n Mid-term Exam n Next Week (Week 5)

3 Topics of Discussion n What Is a Regular Expression? n Expression Modifiers and Simple Statements n Regular Expression Operators n Regular Expression Metacharacters n Unicode

4 Chapter 7: Regular Expressions – Pattern Matching n 7.1 What Is a Regular Expression? n 7.2 Expression Modifiers and Simple Statements n 7.3 Regular Expression Operators

5 7.1 What Is a Regular Expression/ n A regular expression is really just a sequence or pattern of characters that is matched against a string of text when performing searches and replacements. n Example: 7.1 /abc/ ?abc?

6 7.2 Expression Modifiers and Simple Statements n Conditional Modifiers u The if Modifier u Format: Expression2 if Expression1; u Examples: 7.2, 7.3, 7.4 n The DATA Filehandle u Format: __DATA__ The actual data is stored here u Examples: 7.5, 7.6

7 7.2 Expression Modifiers and Simple Statements n The unless Modifier u Format: Expression2 unless Expression1; u Examples: 7.7, 7.8 n Looping Modifiers u The while Modifier u Format: Expression2 while Expression1; u Examples: 7.9 n The Until Modifier u Example: 7.10 n The foreach Modifier u Example: 7.11

8 7.3 Regular Expression Operators n The m Operator and Matching u Format:/Regular Expression/ m#Regular Expression# m(regular expression) u Table 7.1 u Examples: 7.12, 7.13, 7.14, 7.15, 7.16

9 7.3 Regular Expression Operators n The g Modifier-Global Match u Format:m/search pattern/g u Example: 7.17 n The i Modifier-case Insensitivity u Format:m/search pattern/i u Example: 7.18 n Special Scalars for Saving patterns u Example: 7.19 n The x Modifier-Global Match u Example: 7.20

10 7.3 Regular Expression Operators n The s Operator and Substitution u Format:s/old/new/; s/old/new/I; s/old/new/g; u Table 7-2 u Examples: 7.21, 7.22, 7.23 n Changing the Substitution Delimiters u Example: 7.24, 7.25 n The g Modifier-Global Substitution u Examples: 7.26, 7.27

11 7.3 Regular Expression Operators n The I Modifier-Case Insensitivity u Format: s/search pattern/replacement string/i; u Examples: 7.28, 7.29 n The e Modifier-Evaluating An Expression u Format: s/search pattern/replacement string/e; u Examples: 7.30, 7.31, 7.32, 7.33 n Pattern Binding Operators u Format: variable = ~ /Expression/ variable !~ /Expression/ Variable =~ s/old/new u Table 7.3 u Examples: 7.34, 7.35, 7.36, 7.37, 7.38, 7.39

12 Chapter 8: Getting Control – Regular Expression Metacharacters n 8.1 Regular Expression Metacharacters n 8.2 Unicode

13 8.1 Regular Expression Metacharacters n Regular expression metacharacters are characters that do not represent themselves. They are endowed with special powers to allow you to control the search pattern in some way. n Metacharacters lose their special meaning if proceeded with a backslash(\). n Metasymbols – [0-9] = \d n Example: 8.1 /^a…c/ n Table 8.1

14 8.1 Regular Expression Metacharacters n Metacharacters for Single Characters u Table 8.2 u Example: 8.2 n The s Modifier-The Dot metacharacter and the newline u Example: 8.3 n The Character Class u A character class represents one character from a set of characters. u Examples: 8.4, 8.5, 8.6, 8.7, 8.8, 8.9

15 8.1 Regular Expression Metacharacters n The POSIX Character Class u POSIT (the Portable Operating System Interface) is an industry standard used to ensure that programs are portable across operating system. u Table 8.3 u Example 8.11 n Whitespace Metacharacters u Table 8.4 u Examples: 8.12, 8.13, 8.14

16 8.1 Regular Expression Metacharacters n Metacharacters to Repeat Pattern matches u Quantifier – One or more characters u The Greed Factor – the asterisk (*) F It matches for zero or more of the preceding character. F $-=“ab123456783445554437AB” s/ab[0-9]*/X/; XAB u Table 8.5 u Example 8.15, 8.16, 8.17, 8.18, 8.19, 8.20, 8.21, 8.22 n Metacharacters That Turn Off Greediness u By pacing a question mark after a greedy quantifier, the greed is turned off and the search ends after the first match, rather the last one. u Table 8.6 u Examples: 8.24

17 8.1 Regular Expression Metacharacters n Anchoring Metacharacters u Zero-width assertions – Anchors correspond to positions, not actual characters. u Table 8.7 u Example 8.25, 8.26, 8.27, 8.28 n The m Modifier u The m modifier is used to control the behavior of the $ and ^ anchor metacharacters. u Examples: 8.29 n Alternation u Alternation allows the regular expression to contain alternative pattern to be matched, u Example 8.30

18 8.1 Regular Expression Metacharacters n Grouping or Clustering u The process of grouping characters together is called clustering. u Example 8.31, 8.32, 8.33, 8.34 n Remembering or Capturing u Subpattern – If the regular expression pattern is enclosed in parentheses. The subpattern is saved in special numbered scalar variables, and these variables can be used later in the programs. u The process of grouping characters together is called clustering. u Example 8.35, 8.36, 8.37, 8.38, 8.39, 8.40, 8.42

19 8.1 Regular Expression Metacharacters n Turning Off Capturing u ?: metacharacter can be used to suppress the capturing of the subpattern. u Example 8.43 n Metacharacters That Look Ahead and Behind u Look ahead in the string for a pattern (?=pattern) u Look behind in the string for a pattern (?<=pattern) u Table 8.8 u Example 8.44, 8.45, 8.46, 8.47

20 8.1 Regular Expression Metacharacters n The tr or y Function u The tr function translates characters, in a one-on- one correspondence, from the characters in the search string to the characters in the replacement string. u Table 8.9 u Example 8.48 u Example 8.49 (tr Delete Option) u Example 8.50 (tr Complement Option) u Example 8.51 (tr Squeeze Option)

21 8.2 Unicode n The Unicode standard is an effort to solve the problem by creating new character sets, called UTF8 and UTF16. n Unicode has the capacity to encompass all the world’s written language. n Perl and Unicode u Perl 5.6 supports UTF8 Unicode u The utf8 program turns on the Unicode settings and the bytes programs turn off. u Table 8.10 u Example 8.52

22 Summary n What Is a Regular Expression? n Expression Modifiers and Simple Statements n Regular Expression Operators n Regular Expression metacharacters n Unicode

23 Lab n Examples 7.1 – 7.39 (P 163 – 195) n Examples 8.1 - 8.52 (P 197 - 248) n Homework 4

24 Mid-term Exam n Date: Next week n Exam Time: 11:00 AM - 11:30 AM n Contents: Chapter 1- Chapter 8.1.2 n No books, no notes, no computer

25 Next Week n Reading assignment (Textbook chapter 8.1.3 and Chapter 9) n Mid-term Exam (Chapter 1 – Chapter 8.1.2)


Download ppt "Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen."

Similar presentations


Ads by Google