Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming the WWW I CMSC 10100-1 Winter 2004 Lecture 13.

Similar presentations


Presentation on theme: "Introduction to Programming the WWW I CMSC 10100-1 Winter 2004 Lecture 13."— Presentation transcript:

1 Introduction to Programming the WWW I CMSC 10100-1 Winter 2004 Lecture 13

2 2 Today’s Topics Regular expressions (cont’d) CGI scripting discussion

3 3 Midterm Results Total points of the paper: 50 Highest grade: 47.5 Avg. grade: 42.6 10 submitted papers  9 papers with grade >= 40

4 4 Review: Patterns in String Variables Many programming problems require matching, changing, or manipulating patterns in string variables.  An important use is verifying input fields of a form helps provide security against accidental or malicious attacks. For example, if expecting a form field to provide a telephone number as input, your program needs a way to verify that the input comprises a string of seven digits.

5 5 Review: The Match Operator The match operator is used to test if a pattern appears in a string.  It is used with the binding operator (“ =~ ”) to see whether a variable contains a particular pattern.

6 6 Review: Using Character Class Matching any one in a set of characters enclosed within square brackets  foo[bc]ar will match foobar and foocar Ranges can be expressed inside of a character class by using a dash between two characters  [a-g] is equal to [abcdefg]  [0-9] is equal to any digit  [a-zA-Z] Negative character class: use the caret (^) symbol as the first thing in the character class  a[^bc]d, [^0-9]

7 7 Review: The Substitution Operator Similar to the match operator but also enables you to change the matched string.  Use with the binding operator (“ =~ ”) to test whether a variable contains a pattern

8 8 Review: Changing All Occurrences You can place a g (for global substitution) at the end of the substitution expression to change all occurrences of the target pattern string in the search string. For example,  $name = “tom turtle”;  $name =~ s/t/T/g;  print “Name=$name”; The output of this code would be  Name= Tom TurTle Demo http://www.people.cs.uchicago.edu/~wfreis/regex/regex_sub.pl http://www.people.cs.uchicago.edu/~wfreis/regex/regex_sub.pl

9 9 Using Regular Expressions regular expressions to enable programs to match patterns more completely.  They actually make up a small language of special matching operators that can be employed to enhance the Perl string pattern matching.

10 10 The Alternation Operator Alternation operator looks for alternative strings for matching within a pattern.  (That is, you use it to indicate that the program should match one pattern OR the other). The following shows a match statement using the alternation operator (left) and some possible matches based on the contents of $address (right); this pattern matches either com or edu.

11 11 Example Alternation Operator

12 12 Parenthesis For Groupings You use parentheses within regular expressions to specify groupings. For example, the following matches a $name value of Dave or David.

13 13 Special Character Classes Perl has a special set of character classes for short hand pattern matching For example consider these two statements if ( $name =~ m/ / ) { will match $name with embedded space char if ($name =~ m/\s/ ) { will match $name with embedded space, tab, newline

14 14 Special Character Classes

15 15 Special Character Classes - II

16 16 Special Character Classes - III

17 17 Setting Specific Patterns w/ Quantifiers Character quantifiers let you look for very specific patterns For example, use the dollar sign (“ $ ”) to to match if a string ends with a specified pattern. if ($ Name =~ /Jones$/ ) { Matches “John Jones” but not “Jones is here” would not. Also, “The guilty party is Jones” would matches.

18 18 Selected Perl Character Quantifiers I

19 19 Selected Perl Character Quantifiers II

20 20 Selected Perl Character Quantifiers III

21 21 Match the Special Characters Themselves Use a back slash before the special character  \^, \$, \., \?, \(, \), \+, \*,\\,\/ etc  Examples Will a\??bc matches abc, a?bc, bc Will a\++bc matches abc, a+bc, aabc, a++bc

22 22 Building Regular Expressions That Work Regular expressions are very powerful— but they can also be virtually unreadable.  When building one, tart with a simple regular expression and then refine it incrementally. Build a piece and then test  The following example will build a regular expression for a date checker dd/mm/yyyy format (for example, 05/05/2002 but not 5/12/01 ).

23 23 1. Determine the precise field rules. - What is valid input and what is not valid input?  E.g., For a date field, think through the valid and invalid rules for the field.  You might allow 09/09/2002 but not 9/9/2002 or Sep/9/2002.  Work through several examples as follows: Building Regular Expressions That Work

24 24 Work through several examples

25 25 Building Regular Expressions that Work 2. Get form and form-handling programs working  Build a sending form the input field  Build the receiving program that accepts the field.  For example, a first cut receiving program: $date = param(‘udate’); if ( $date =~ m/.+/ ) { print ‘Valid date=’, $date; } else { print ‘Invalid date=’, $date; } Any Sequence of characters

26 26 Building Regular Expressions that Work 3. Start with the most specific term possible.  For example, slashes must always separate two characters (for the month), followed by two more characters (for the day), followed by four characters (for the year). if ( $date =~ m{../../....} ) { Any 2 characters Any 2 characters Any 4 characters

27 27 Building Regular Expressions that Work 4. Anchor and refine. (Use ^ and $ when possible)  if ( $date =~ m{^\d\d/\d\d/\d\d\d\d$} ) { Starts with 2 digits in middle Ends with 4 digits

28 28 Building Regular Expressions that Work 5. Get more specific if possible.  The first digit of the month can be only 0, 1, 2 or 3. For example, 05/55/2002 is clearly an illegal date.  Only years from this century are allowed. Because we don’t care about dates like 05/05/1999 or 05/05/3003.

29 29 Add these rules below if ( $date =~ m{^\d\d/[0-3]\d/2\d\d\d$} ) {  Now the regular expression recognizes input like 09/99/2001 and 05/05/4000 as illegal. Year starts with a “2” Month starts with a “0-3” Building Regular Expressions that Work Use { } instead of / / as delimiter

30 30 Tip: Regular Expression Special Variables Perl regexs set several special scalar variables:  $& will be equal to the first matching text  $` will be the text before the match, and  $’ will be the text after the first match. $name='*****Marty'; if ( $name =~ m/\w/ ) { print "got match at=$& "; print "B4=$` after=$'"; } else { print "Not match"; } would output: got match at=M B4=***** after=arty

31 31 Full Example Program 1. #!/usr/local/bin/perl 2. use CGI ':standard'; 3. print header, start_html('Date Check'); 4. $date=param('udate'); 5. if ($date =~ m{^\d\d/[0-3]\d/2\d\d\d$}){ 6. print 'Valid date=', $date; 7. } else { 8. print 'Invalid date=', $date; 9. } 10. print end_html;

32 32 Would Output The Following...

33 33 CGI Scripting Discussion HW4 Problems  http://www.classes.cs.uchicago.edu/classes/archive/2 004/winter/10100-1/02/hw4/requirements.txt http://www.classes.cs.uchicago.edu/classes/archive/2 004/winter/10100-1/02/hw4/requirements.txt HW5 Problems  http://www.classes.cs.uchicago.edu/classes/archive/2 004/winter/10100-1/02/hw5/requirements.txt http://www.classes.cs.uchicago.edu/classes/archive/2 004/winter/10100-1/02/hw5/requirements.txt


Download ppt "Introduction to Programming the WWW I CMSC 10100-1 Winter 2004 Lecture 13."

Similar presentations


Ads by Google