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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

Regular Expressions in Perl By Josue Vazquez. What are Regular Expressions? A template that either matches or doesn’t match a given string. Often called.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
Asp.NET Core Vaidation Controls. Slide 2 ASP.NET Validation Controls (Introduction) The ASP.NET validation controls can be used to validate data on the.
Regular Expression (1) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
More Regular Expressions. List/Scalar Context for m// Last week, we said that m// returns ‘true’ or ‘false’ in scalar context. (really, 1 or 0). In list.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Guide To UNIX Using Linux Third Edition
Regular Expressions Regular Expression (or pattern) in Perl – is a template that either matches or doesn’t match a given string. if( $str =~ /hello/){
Scripting Languages Chapter 8 More About Regular Expressions.
More on Regular Expressions Regular Expressions More character classes \s matches any whitespace character (space, tab, newline etc) \w matches.
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Last Updated March 2006 Slide 1 Regular Expressions.
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Regular Expressions Dr. Ralph D. Westfall May, 2011.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Lecture 8 perl pattern matching features
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Input, Output, and Processing
Finding the needle(s) in the textual haystack
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 13.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Regular Expressions.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
CPTG286K Programming - Perl Chapter 7: Regular Expressions.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 12.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Chapter 18 The HTML Tag
Regular Expressions Copyright Doug Maxwell (
Finding the needle(s) in the textual haystack
Regular Expressions 'RegEx'.
CS 330 Class 7 Comments on Exam Programming plan for today:
Looking for Patterns - Finding them with Regular Expressions
Finding the needle(s) in the textual haystack
Finding the needle(s) in the textual haystack
Pattern Matching in Strings
Advanced Find and Replace with Regular Expressions
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13

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

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

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

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 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 Example Alternation Operator

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 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 Special Character Classes

15 Special Character Classes - II

16 Special Character Classes - III

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 Selected Perl Character Quantifiers I

19 Selected Perl Character Quantifiers II

20 Selected Perl Character Quantifiers III

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 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 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 Work through several examples

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 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 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 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 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 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 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 Would Output The Following...

33 CGI Scripting Discussion HW4 Problems  004/winter/ /02/hw4/requirements.txt 004/winter/ /02/hw4/requirements.txt HW5 Problems  004/winter/ /02/hw5/requirements.txt 004/winter/ /02/hw5/requirements.txt