Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.

Slides:



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

Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 1-1 The Web Wizards Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizards Guide to PHP by David Lash.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizard’s Guide to PHP by David Lash.
Data Manipulation & Regular Expressions CSCI 215.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 Controlling Script Flow! David Lash Chapter 3 Conditional Statements.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Chapter 5: Loops and Files.
Scripting Languages Chapter 8 More About Regular Expressions.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
1 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and Custom Forms.
Last Updated March 2006 Slide 1 Regular Expressions.
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
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.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
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.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
Enhancing Forms with OLE Fields, Hyperlinks, and Subforms – Project 5.
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
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
CS346 Regular Expressions1 Pattern Matching Regular Expression.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Perl Day 4. Fuzzy Matches We know about eq and ne, but they only match things exactly We know about eq and ne, but they only match things exactly –Sometimes.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Linux Administration Working with the BASH Shell.
Validation Controls Assist your users with providing the correct type of input for your application Assist your users with providing the correct type of.
Section 10.1 Define scripting
Loops BIS1523 – Lecture 10.
Lecture 9 Shell Programming – Command substitution
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Conditions and Ifs BIS1523 – Lecture 8.
The Web Wizard’s Guide to PHP
Data Manipulation & Regex
PHP PART 2.
Validation using Regular Expressions
Presentation transcript:

Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash

Copyright © 2003 Pearson Education, Inc. Slide 6a-2 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns

Copyright © 2003 Pearson Education, Inc. Objectives To learn to use PHP pattern-matching functions and regular expressions to match character string patterns and filter input data

Copyright © 2003 Pearson Education, Inc. Slide 6a-4 Why Match Patterns Scripting problems may require: verification of input from form was input a 7 digit phone number parsing input from a file FirstName:LastName:Age:Salary PHP supports three pattern matching functions: preg_match, preg_split(), and preg_replace(). Regular expressions are used to define very specific match patterns

Copyright © 2003 Pearson Education, Inc. Slide 6a-5 The preg_match() function Use preg_match() to check if a string contains a match pattern:

Copyright © 2003 Pearson Education, Inc. Slide 6a-6 For example Consider the following $name = 'Jake Jackson'; $pattern = 'ke'; if (preg_match($pattern, $name)){ print 'Match'; } else { print 'No match'; } This code outputs “ Match” since the string “ke” is found. if $pattern was “aa” the above code segment would output “ No match”

Copyright © 2003 Pearson Education, Inc. Slide 6a-7 What are regular expressions? Special pattern matching characters with specific pattern matching meanings. For example, a caret symbol (^) returns a match when the pattern that follows starts the target string. $part = 'AA100'; $pattern = '^AA'; if (preg_match($pattern, $part)) { print 'Match'; } else { print 'No match'; } Check if $part starts with “AA” Would be output if $part was “AB100”, “100AA”, or “Apple”.

Copyright © 2003 Pearson Education, Inc. Slide 6a-8 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-9 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-10 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-11 Selected Pattern Matching Characters

Copyright © 2003 Pearson Education, Inc. Slide 6a-12 For example... The following uses preg_match() (case insensitive compare) Enter product code (Use AA## format): Please enter description: Asks for a product code and description (not to contain “Boat” or “Plane”.

Copyright © 2003 Pearson Education, Inc. Slide 6a-13 A Full Script Example Consider an example script that enables an end- user to select multiple items from a checklist. A survey about menu preferences

Copyright © 2003 Pearson Education, Inc. Slide 6a-14 A Full Example Show Source Code

Copyright © 2003 Pearson Education, Inc. Slide 6a-15 The Output... The previous code can be executed at:

Copyright © 2003 Pearson Education, Inc. Slide 6a-16 Using grouping characters Use parentheses to specify a group of characters in a regular expression. Above uses parentheses with “|” to indicate “Dav” can be followed by “e” or “id”.

Copyright © 2003 Pearson Education, Inc. Slide 6a-17 Using grouping characters Now add in “^” and “$” characters...

Copyright © 2003 Pearson Education, Inc. Slide 6a-18 Use curly brackets to specify a range of characters Use curly brackets to look for a repeating character E.g., L{3} matches 3 “L”s L{3,} matches3 or more “L”s L{2,4} matchs 2 to 4 “L”s

Copyright © 2003 Pearson Education, Inc. Slide 6a-19 Use square brackets for character classes Use square brackets to match one of the characters located inside

Copyright © 2003 Pearson Education, Inc. Slide 6a-20 Use square brackets for range Its more common to specify a range of matches For exampe [0-9], [a-z] or [A-Z] Or use multiple characters at once...

Copyright © 2003 Pearson Education, Inc. Slide 6a-21 Using caret “^” and square brackets When caret “^” is the first character within square brackets it means “not”. Note: Within a character class, as in [^... ], “^” means not. Earlier we saw how it can indicate that the character that follows the caret symbol starts the match pattern

Copyright © 2003 Pearson Education, Inc. Slide 6a-22 Special Pre-defined character classes

Copyright © 2003 Pearson Education, Inc. Slide 6a-23 Special Pre-defined character classes

Copyright © 2003 Pearson Education, Inc. Slide 6a-24 Special Pre-defined character classes

Copyright © 2003 Pearson Education, Inc. Slide 6a-25 Building Regular Expressions that work Building Regular expressions is best done incrementally Lets look at a process to build a regular expression to validate a date input field: mm/dd/yyyy format (for example, 01/05/2002 but not 1/5/02 ).

Copyright © 2003 Pearson Education, Inc. Slide 6a-26 Building an Example Regular Expression 1. Determine the precise field rules. What is valid input and invalid input? You might decide to allow 09/09/2002 but not 9/9/2002 or Sep/9/2002 Work through several examples as follows:

Copyright © 2003 Pearson Education, Inc. Slide 6a-27 Building an Example Regular Expression 2. Get the form and form-handling scripts working Build the input form and a “bare bones”receiving script For example, the following receives input of 1 or more characters: if ( preg_match( ‘.+’, $date ) ) { print "Valid date= $date"; } else { print "Invalid date= $date"; }

Copyright © 2003 Pearson Education, Inc. Slide 6a-28 Building an Example Regular Expression 3. Start with the most specific term possible You know the input must have 2 slashes between a 2 character month, 2 character day and 4 character year So change the receiving script to: if ( preg_match( ‘../../....’, $date ) ) { print "Valid date= $date"; } else { print "Invalid date= $date"; } So 12/21/1234 and fj/12/ffff are valid, but 1/1/11 is not.

Copyright © 2003 Pearson Education, Inc. Slide 6a-29 Building an Example Regular Expression 4. Anchor the parts you can. Add the “ ^” and “ $” quantifiers where possible. Also, we can add the [[:digit:]] character class to require numbers instead of any character. So change receiving script to: $two=‘[[:digit:]]{2}’; if ( preg_match("^$two/$two/$two$two$", $date ) ) { print "Valid date= $date"; } else { print "Invalid date= $date"; } So 01/16/2003, 09/09/2005, 01/12/1211, and 99/99/9999 are valid dates.

Copyright © 2003 Pearson Education, Inc. Slide 6a-30 Building an Example Regular Expression 5. Get more specific if possible... You might note that three more rules can be added: The first digit of the month can be only 0, or 1. For example, 25/12/2002 is clearly illegal. The first digit of a day can be only 0, 1, 2, or 3. For example, 05/55/2002 is clearly illegal. Only allow years from this century. Don’t care about dates like 05/05/1928 or 05/05/3003. $two=‘[[:digit:]]{2}’; $month=‘[0-1][[:digit:]]’; $day=‘[0-3][[:digit:]]’; $year=“20$two"; if ( preg_match("^($month)/($day)/($year)$", $date ) ) { Now input like 09/99/2001 and 05/05/4000 is illegal.

Copyright © 2003 Pearson Education, Inc. Slide 6a-31 A Full Script Example Consider an example script that asks an end- user for a date Use regular expressions to validate Use the following HTML input

Copyright © 2003 Pearson Education, Inc. Slide 6a-32 A Full Example Show Source Code

Copyright © 2003 Pearson Education, Inc. Slide 6a-33 The Output... The previous code can be executed at:

Copyright © 2003 Pearson Education, Inc. Slide 6a-34 Matching Patterns With preg_split() Use preg_split() to break a string into different pieces based on the presence of a match pattern.

Copyright © 2003 Pearson Education, Inc. Slide 6a-35 Matching Patterns With preg_split() Consider another example; $line = ‘Baseball, hot dogs, apple pie’; $item = preg_split( ‘,’, $line ); print ("0=$item[0] 1=$item[1] 2=$item[2]"); These lines will have the following output: 0=Baseball 1= hot dogs 2= apple pie

Copyright © 2003 Pearson Education, Inc. Slide 6a-36 Matching Patterns With preg_split() When you know how many patterns you are interested in, you can use list() along with preg_split(): line = ‘AA1234:Hammer:122:12’; list($partno, $part, $num, $cost) = preg_split(‘:’, $line, 4); print "partno=$partno part=$part num=$num cost=$cost"; The above code would output the following: partno=AA1234 part=Hammer num=122 cost=12

Copyright © 2003 Pearson Education, Inc. Slide 6a-37 Example of preg_split() As an example of preg_split() consider the following: $line = ‘Please, pass thepepper’; $result = preg_split( ‘[[:space:]]+’, $line ); Will results in the following: $result[0] = ‘Please’; $result[1] = ‘,’ $result[2] = ‘pass’; $result[3] = ‘thepepper’;

Copyright © 2003 Pearson Education, Inc. Slide 6a-38 A Full Script Example Consider an example script that updates the date checker just studied: Uses preg_split() to further refine date validation Uses the same input form:

Copyright © 2003 Pearson Education, Inc. Slide 6a-39 A Full Example Show Source Code

Copyright © 2003 Pearson Education, Inc. Slide 6a-40 The Output... The previous code can be executed at:

Copyright © 2003 Pearson Education, Inc. Slide 6a-41 Using preg_replace() Use preg_replace() when replacing characters in a string variable. It can be used to replace one string pattern for another in a string variable. For example: $start = ‘AC1001:Hammer:15:150’; $end = preg_replace( ‘Hammer’, ‘Drill’, $start ); print "end=$end"; The above script segment would output : end=AC1001:Drill:15:150

Copyright © 2003 Pearson Education, Inc. Slide 6a-42 Summary PHP supports a set of operators and functions that are useful for matching and manipulating patterns in strings: The preg_match() function looks for and match patterns The preg_split() function uses a pattern to split string values into as many pieces as there are matches. Regular expressions greatly enhance its pattern matching capabilities.