1 CO3041 Databases and the Web Week 8 Dr James Denholm-Price.

Slides:



Advertisements
Similar presentations
Session 3BBK P1 ModuleApril 2010 : [#] Regular Expressions.
Advertisements

FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
BBK P1 Module2010/11 : [‹#›] Regular Expressions.
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/28.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Last Updated March 2006 Slide 1 Regular Expressions.
PHP : Hypertext Preprocessor
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.
Tutorial 14 Working with Forms and Regular Expressions.
Regular Expressions Dr. Ralph D. Westfall May, 2011.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
 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.
Creating databases for web applications Play quizzes Testing process regular expressions: form validation PHP coding handling forms Homework: regular expressions.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
INFO 320 Server Technology I Week 7 Regular expressions 1INFO 320 week 7.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009.
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
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,
By Michael Wolfe. Grouping Things and Hierarchical Matching  In a regexp ab|ac is nice, but it’s not very efficient because it uses “a” twice  Perl.
Programming in Perl regular expressions and m,s operators Peter Verhás January 2002.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
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.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
By: Andrew Cory. Grouping Things & Hierarchical Matching Grouping characters – ( and ) Allows parts of a regular expression to be treated as a single.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
Powerpoint Templates Page 1 Powerpoint Templates GROUP 8:REGULAR EXPRESSION GURU BESAR: PN. SARINA SULAIMAN CIKGU-CIKGU: 1.CIKGU NENI 2.CIKGU
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida Information Management.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
1 Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
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.
NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image. ADVANCED.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Variable Variables A variable variable has as its value the name of another variable without $ prefix E.g., if we have $addr, might have a statement $tmp.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Looking for Patterns - Finding them with Regular Expressions
Lecture 19 Strings and Regular Expressions
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 ©
Data Manipulation & Regex
Validation using Regular Expressions
Presentation transcript:

1 CO3041 Databases and the Web Week 8 Dr James Denholm-Price

CO3041 Databases and the Web lecture 8 2 Outline/Schedule Today (lecture 8/week 9) –In-class test 1: tough questions –Dates (from last time) –Perl-compatible regular expressions Next week (lecture 9/week 10) –Discussing a set of “requirements” for the Group Project that will form the marking scheme c.f. last year’s. –Edit/Delete

In-class test 1 Good results Tough questions 3CO3041 Databases and the Web lecture 8

In-class test 1 – Good results:  =54% CO3041 Databases and the Web lecture

5 From last time Date query efficiency

Dates MySQL default date format is ‘ YYYY-MM-DD HH:MM:SS ’ –Other formats are accepted…Other formats SQL queries can be formed comparing date/times, e.g. –SELECT * FROM tableName WHERE dateField > ' :30:00' Output from MySQL queries can be manipulated with PHP’s date functions but PHP is best suited to comparing numbers using “>”, “<” and “==” date functions –The most common number format for dates is a Unix timestamp which is a 32bit (signed) integer holding the number of seconds since 1 st January –Right about.. Now! MySQL: NOW() in string format, UNIX_TIMESTAMP() as a numberMySQL PHP: time() in Unix timestamp format, date() in string formatPHP time date –In PHP strtotime($row['dateField']); converts a MySQL DATETIME string to a Unix epoch number strtotime Also good for doing things like strtotime('+7 days', time() ) to calculate the date & time 7 days hence… CO3041 Databases and the Web lecture 8 6

Working in MySQL with PHP MySQL functions –UNIX_TIMESTAMP converts DATETIME into these numbers –FROM_UNIXTIME converts the number back to a DATETIME string format QEasy to convert to/from date strings and timestamps… These two queries are equivalent, but which is the most efficient? 1.$sql = "SELECT dateField FROM tableName WHERE UNIX_TIMESTAMP(dateField) > $PHPdateTime"; or 2.$sql = "SELECT dateField FROM tableName WHERE dateField > FROM_UNIXTIME($PHPdateTime)"; CO3041 Databases and the Web lecture 8 7

Working in MySQL MySQL functions –UNIX_TIMESTAMP converts DATETIME into these numbers –FROM_UNIXTIME converts the number back to a DATETIME string format Q:Easy to convert to/from date strings and timestamps… These two queries are equivalent, but which is the more efficient? 1.$sql = "SELECT dateField FROM tableName WHERE UNIX_TIMESTAMP(dateField) > $PHPdateTime"; or 2.sql = "SELECT dateField FROM tableName WHERE dateField > FROM_UNIXTIME($PHPdateTime)"; A:One applies a function to your database (e.g rows!) the other applies a function to the comparison element … which is one function call irrespective of the number of rows! (demo – please don’t try this too often!)demo CO3041 Databases and the Web lecture 8 8

Manipulating dates in PHP date(string format, [integer timestamp])date –optional timestamp defaults to ‘now’ –format uses letters to represent a lot of date/time elements –E.g. to get back to MySQL’s “YYYY-MM-DD HH:MM:SS” use date('Y-m-d H:i:s') –To put keyword characters into the date string they must be escaped. –E.g. date('D\&\n\b\s\p;d-M- y\&\n\b\s\p;\a\t\&\n\b\s\p;g.ia',$row->startTime) D = Mon, Tue, Wed, … d = 01, …, {28,29,30,31} M = Jan, Feb, Mar, … y = 04 for 2004 g = 1…12 hours, i = 01 … 60 minutes, a = {am,pm} CO3041 Databases and the Web lecture 8 9

10 Regular Expressions Perl-compatible syntax

CO3041 Databases and the Web lecture 8 11 Regular Expressions Rules for matching string patterns. PHP’s REs (regexps) are strings like '/hello/' –This is a ‘literal character’ RE that matches the word “hello” at any point in a string. –E.g. In 'Fred said “hello”!' (url)url –There are a number of special characters that mean something in a regexp and otherwise must be escaped with a backslash “ \ ”. These are: ^ $. * + ? = ! : | \ / ( ) [ ] { } –These form the basis of complex regexps that match flexible patterns rather than fixed strings. If all you want is to match a fixed string the manual recommends using strpos() or strstr()the manual –In PHP you’ll have to escape quotes too.

CO3041 Databases and the Web lecture 8 12 Regular Expressions: Test $string="Hello World"; –preg_match('/world/', $string); TRUE/FALSE? –preg_match('/o W/', $string); TRUE/FALSE? –preg_match('/oW/', $string); TRUE/FALSE? –preg_match('/o/', $string); TRUE/FALSE?

CO3041 Databases and the Web lecture 8 13 Regular Expressions: Test $string="Hello World."; –preg_match('/world/', $string); Doesn’t match case (“W” not “w”) –preg_match('/o W/', $string); Matches the $matches array is ['o W'] –preg_match('/oW/', $string); Doesn’t match (no space) –preg_match('/o/', $string); Matches just the “o” in “Hello”

CO3041 Databases and the Web lecture 8 14 RE: Escaped special characters $string="Hello+world."; –preg_match('/o+w/', $string); Doesn’t match case as “ + ” is a special character (meta-character.) –preg_match('/o\+w/', $string); Matches as “ \+ ” is escaped.

CO3041 Databases and the Web lecture 8 15 RE: Anchors ‘^’ and ‘$’ $string="Hello World"; –preg_match('/^Hello/', $string); Matches “ ^ ” means beginning of string. –preg_match('/^World/', $string); No match. –preg_match('/World$/', $string); Matches “ $ ” means end of string. –preg_match('/^World$/', $string); No match – “ ^ ” anchors to the start of the string and “ $ ” to the end.

CO3041 Databases and the Web lecture 8 16 RE: Character classes ‘[]’ $string="bat cat rat"; –preg_match('/cat/', $string); Matches “ cat ”. –preg_match('/[bcr]at/', $string); Matches “ bat ” first but would also match “ bat ”, “ cat ” and “ rat ”. preg_match_all() would match them all. –preg_match('/[cat]/', $string); Stops at the first match it finds which is the “ a ” in “ bat ” –The square brackets mean “any character in the class” i.e. in this case “any one of ‘ c ’, ‘ a ’ or ‘ t ’ not ‘ c ’, ‘ a ’ and ‘ t ’.”

CO3041 Databases and the Web lecture 8 17 RE: Negating classes ‘[^…]’ $string="bat cat rat"; –Starting a character class with a caret “ ^ ” negates the class, i.e. it (the class) matches anything but its contents. –preg_match('/[^br]at/', $string); Matches just “ cat ” because the class means “anything but ‘ b ’ or ‘ r ’.”(url)url –However a caret inside a class becomes a member of that class so: –preg_match('/[b^r]at/', $string); Matches “ bat ” first (“ rat ” second) and would also match "^at"

CO3041 Databases and the Web lecture 8 18 RE: Matching case $string=" yes Yes yEs yeS YEs YeS "; –preg_match('/[Yy][Ee][Ss]/', $string); Matches any combination of upper/lower “ yes ” –Much easier to choose a case-insensitive match with a modifier at the end of the RE: –preg_match('/yes/i', $string); Q: Does this match? –preg_match('/YES/i', $string); A: Yes! It’s a case-insensitive match so even though “ YES ” isn’t in $string it still matches the first “ yes ” (url)url

CO3041 Databases and the Web lecture 8 19 RE: Character ranges $string=" yes1 Yes2 yEs3 yeS4 YEs5 YeS6 "; –preg_match('/yes[1-6]/i',$string); Matches “ yes1 ” at first (url).url “ [1-6] ” does not match “ 1 ”, “ - ”, “ 6 ” individually, it matches the range of ASCII characters from “ 1 ” to “ 6 ” –preg_match_all('/yes[1-6]/i', $string); Matches all of the combinations (url).url Q:Does preg_match('/[0-7]/i','007'); return TRUE or FALSE? A:TRUE. The character range [0-7] matches any 1 character...

CO3041 Databases and the Web lecture 8 20 RE: Shorthand classes Perl REs define shorthand classes: –\d digits, the same as [0-9] –\s spaces, including spaces, tabs… –\w ‘word’ (alphanumeric) [a-zA-Z0-9_] –. matches anything except “ \n ” Capital letters negate the class –\D[^0-9] –\S[^\s] –\W[^a-zA-Z0-9_]

CO3041 Databases and the Web lecture 8 21 RE: Alternatives “|” $string=" yes1 Yes2 yEs3 yeS4 YEs5 YeS6 "; –preg_match('/yes4|yes3/i',$string); It’s case-insensitive and the “ | ” means ‘or’ preg_match matches & returns the first match in the string, which is “ yEs3 ”. preg_match_all matches & returns all the matches which are “ yEs3 ” and “ yeS4 ”. Q:So what regexp would match either ‘Brosnan’ or ‘Connery’? A: /Brosnan|Connery/ (url)url

CO3041 Databases and the Web lecture 8 22 RE: Grouping “(…)” $string=" KT1 2EE "; –preg_match('/(...)\s(...)/',$string); Remember “. ” matches any character (including a space) The brackets group the character matches into subexpressions preg_match returns these in the $matches array as elements [1] and [2] (url)url $string=" "; Q:how can I extract the codes from the number ‘xxxyyyyzzzz’ with a regexp? A: /(\d\d\d)(\d\d\d\d)(\d\d\d\d)/

CO3041 Databases and the Web lecture 8 23 RE: Repeating “?” “*” “+” “{m,n}” ? “ 0 or 1 ” * “any number from 0 up” + “at least 1 ” {n} “exactly n ” {m,n} “between m and n ” $string=" KT1 2EE "; –preg_match('/.{3}\s.{3}/',$string); Matches postcode but also rubbish like “ !!! !!! ” Q:How do we restrict “. ” to alphanumeric? A:Use “ \w ” instead of “. ”

CO3041 Databases and the Web lecture 8 24 RE: Greedy matching Posix and default Perl regexps are “greedy” –Any RE will be matched at the earliest position in the string. –The maximal quantifiers ?, *, +, {n,m} will match as much of the string as possible while allowing the regex to match. –If there are two or more elements in a regexp, the leftmost greedy quantifier, if any, will match as much of the string as possible while still allowing the whole regexp to match. –In an alternation the leftmost alternative that allows a match will be the one used. In PHP/PCRE non-greedy matching (stops as soon as a match finishes) needs the “U” modifier.

CO3041 Databases and the Web lecture 8 25 Regexp greediness $string="How many Bonds have there been?" preg_match('/(.*)n(.*)/', $string, $matches) –“. ” matches any character (except newline “ \n ”) –“ * ” means “any number of the preceding character” –So the regexp tries to match two groups of characters separated by an “ n ” –Greedy (matches as much as possible): (url)url $match[0] == $string as the regexp matches everything! $match[1] == "How many Bonds have there bee" $match[2 ] == "?" –Ungreedy (regexp is '/(.*)n(.*)/U') stops as soon as possible: (url)url $match[0] == "How man" $match[1] == "How ma" $match[2 ] == "" matches nothing as we used “ * ”

CO3041 Databases and the Web lecture 8 26 Quick exercise: RE for last names form.html First name> Last name process.php <?php $fn_OK = preg_match( '/^[A-Za-z]+$/', $_POST['firstname'] ); $ln_OK = preg_match( '/ /', $_POST['lastname'] ); if($fn_OK && $ln_OK) { $sql = "INSERT INTO name VALUES('{$_POST['firstname']}', '{$_POST['lastname']}'"; if(!mysql_query($sql)) die(' Error: '. mysql_error()); else echo ' OK! ; ?> url url srcsrc

27 Complex validation PHP and Regular Expressions

CO3041 Databases and the Web lecture 8 28 Quick intro to PHP RE functions PHP includes a “PCRE” library –Perl-Compatible Regular Expressions … next slide … + supports Unix standard “Posix” RE’s PCRE is a superset of Posix –the manual says they’re faster so use them for preference!the manual –PCRE are very similar to Perl (& JavaScript) REs –Posix RE functions (briefly) [look them up online]look them up online –bool ereg ( string pattern, string string [, array regs]) eregi () case-insensitive version –string ereg_replace ( string pattern, string replacement, string string) eregi_replace() case-insensitive version –array split ( string pattern, string string [, int limit]) spliti()

CO3041 Databases and the Web lecture 8 29 Main PHP PCRE functions –int preg_match ( string pattern, string subject [, array matches [, int flags [, int offset]]] ) The basic pattern-matching function. Returns: –‘ 0 ’ for no match ( 0==FALSE ) –‘ 1 ’ for a match (stops at the first match)( 1==TRUE ) –‘ FALSE’ for an error. ( 0!==FALSE ) Returns the matching strings in the array matches : –matches[0] == full pattern match; matches[1] sub-pattern 1 etc.

CO3041 Databases and the Web lecture 8 30 Main PHP PCRE functions –int preg_match_all ( string pattern, string subject, array matches [, int flags [, int offset]] ) Unlike preg_match() this doesn’t stop at the first match. Returns a count of all matches. (and ‘ FALSE ’ for error.) Returns the matching strings in the array matches : –matches[0] is an array of the full pattern matches –matches[1] an array of sub-pattern 1 matches …etc.

CO3041 Databases and the Web lecture 8 31 Main PHP PCRE functions –mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit] ) Search and replace by RE. pattern is the RE. replacement may reference the RE sub-patterns as $n or \\n limit limits :-) the number of replacements. Very useful for removing invalid characters. –array preg_split ( string pattern, string subject [, int limit [, int flags]] ) String splitting by RE. Returns an array of string fragments –optionally up to limit …

CO3041 Databases and the Web lecture 8 32 PHP RE’s PHP patterns are strings, usually delimited by pairs of forward slashes –E.g. $pattern = "/world/"; –May be delimited by other pairs of characters. –No case-insensitive PCRE functions -- just add an “i” after the end pattern delimiter. $pattern="/world/i"; Complex RE’s may be copied from regexlib.comregexlib.com –E.g. one of the simpler address matching RE’s is You can test RE’s here in JavaScript: –staffnet/~ku13043/WebDB/ex/week05/regexp_test.htmlstaffnet/~ku13043/WebDB/ex/week05/regexp_test.html Or here in PHP: –staffnet/~ku13043/WebDB/ex/week05/RE_test_post.phpstaffnet/~ku13043/WebDB/ex/week05/RE_test_post.php

CO3041 Databases and the Web lecture 8 33 Form entry demo with REs <?php //Submitted the form? if (isset($_POST['Submit']) && $_POST['Submit']=='Submit…') { $formSubmitted=1; //Valid? Both fields are required! //And should be text only //Valid? Both fields are required! if ( preg_match('/^\w+\-?\w+$/', $_POST['firstname']) && preg_match('/^\w+\-?\w+$/', $_POST['lastname']) ) $formValid=1; else $formValid=0; else$formValid=0; } else $formSubmitted=0; ?>

CO3041 Databases and the Web lecture 8 34 Form entry demo with REs First name: <input type="text" maxlength="30" name="firstname" <?php if ($formSubmitted && !$formValid) echo 'value="'. $_POST['firstname']. '"'; ?> /> <?php if ($formSubmitted && !$formValid && !preg_match('/^\w+\-?\w+$/', $_POST['firstname']) ) echo ' Please enter your '. 'first name using only letters! '; ?>