CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.

Slides:



Advertisements
Similar presentations
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
Advertisements

1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
1 CSE 303 Lecture 7 Regular expressions, egrep, and sed read Linux Pocket Guide pp , 73-74, 81 slides created by Marty Stepp
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller
Regular expressions (contd.) -- remembering subpattern matches When a is being matched with a target string, substrings that match sub-patterns can be.
CS 330 Programming Languages 10 / 10 / 2006 Instructor: Michael Eckmann.
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Scripting Languages Chapter 8 More About Regular Expressions.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
slides created by Marty Stepp
Regex Wildcards on steroids. Regular Expressions You’ve likely used the wildcard in windows search or coding (*), regular expressions take this to the.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Last Updated March 2006 Slide 1 Regular Expressions.
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’
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.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
REGULAR EXPRESSIONS. Lexical Analysis Lexical analysers can be constructed by programs such as LEX These programs employ as input a description of the.
Chapter 11: Regular Expressions and Matching The match operator has the following form. m/pattern/ A pattern can be an ordinary string or a generalized.
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,
CPSC 388 – Compiler Design and Construction Scanners – JLex Scanner Generator.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expression Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
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
JavaScript, Part 2 Instructor: Charles Moen CSCI/CINF 4230.
Regular Expressions What is this line all about? while (!($search =~ /^\s*$/)) { It’s a string search just like before, but with a huge twist – regular.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
Appendix A: Regular Expressions It’s All Greek to Me.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
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,
©Brooks/Cole, 2001 Chapter 9 Regular Expressions.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
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.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 5 Regular Expressions.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
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.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Regular Expressions Copyright Doug Maxwell (
RE Tutorial.
Regular Expressions and perl
Pattern Matching in Strings
Advanced Find and Replace with Regular Expressions
Selenium WebDriver Web Test Tool Training
Data Manipulation & Regex
CIT 383: Administrative Scripting
Lecture 25: Regular Expressions
REGEX.
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions

Regular Expressions  For defining patterns to match strings  PHP supports two kinds of regular expressions  Regular Expressions (Perl-Compatible)   Regular Expression (POSIX Extended) 

Meta-characters inside a regular expression \ For escaping special characters ^ Matches the beginning of a string $ Matches the end of a string. Matches any single character except newline | Alternatives (OR) [ Start of a class ] End of a class ( Start of a sub-pattern ) End of a sub-pattern { Start of a qualifier } End of a qualifier

Regular Expression Examples  /ab/ -- matches any string containing "ab"  e.g., cab, fabulous  /^ab/ -- matches any string that begins with "ab"  e.g., absolute, abnormal, abc  /ab$/ -- matches any string that ends with "ab"  e.g., cab, matlab  /^ab$/ -- matches only the string "ab"  /yes|no/ or /(yes)|(no)/  both match either "yes" or "no"

Regular Expression Examples  /^c(a|u|ur)b$/ -- matches "cab", "cub", or "curb"  /^c.b$/ -- matches "cab", "cub", "cxb", "c3b", etc.  /\\/ -- matches a single back slash character in a string.  When written as a PHP string, you need to write "/\\\\/"

Quantifiers – To specify the quantity ? 0 or 1 * 0 or more + 1 or more {x} Exactly x occurrences {x, y} Between x and y occurrences (inclusive) {x,} At least x occurrences

Regular Expression Examples  /^a*b$/ -- matches "b", "ab", "aab", "aaaaab", etc.  /^a.*b$/ -- matches any string that begins with 'a' and ends with 'b'  /c.+t/ -- matches any substring with at least one characters between 'c' and 't'  e.g., "cat", "cart", "comet", "cccccccct", etc. (but not "ct")  /^(0|1|2|3|4|5|6|7|8|9){5}$/  Matches any 5-digit number

Character Classes  A class is created by placing characters within square brackets ('[' and ']'). The resulting pattern matches any single character belong to the class. e.g.,  [aeiou] – matches any lowercase vowel  [ABCabc] – matches 'A', 'B', 'C', 'a', 'b', or 'c'  You can use hyphen '-' to specify a range of characters within the square brackets. e.g.  [A-Z] – matches any of the uppercase letters  [A-Za-z0-9] – matches any of the alphanumeric characters  '^' is a negation operator when used as the first character in the class.  e.g., [^0-9] – matches any character that is not a digit

Commonly Used Character Classes CLASSSHORTCUTMeaning [0-9]\d Any digit [\f\r\t\n\v]\s Any white space [A-Za-z0-9_]\w Any word character (alphanumeric and '_') [^0-9]\D Not a digit [^\f\r\t\n\v]\S Not a white space [^A-Za-z0-9_]\W Not a word character e.g.,  /^[0-9]{5}$/ or /^\d{5}$/  Matches any 5-digit number

Modifiers  These characters, when placed after the closing delimiter, alter the behavior of a regular expression. A Anchors the pattern to the beginning of the string i Enable case-insensitive mode m Enables multiline matching s Has the period match every character, including newline x Ignores most white space U Performs a non-greedy match (i.e., matches as little data as possible)  A more complete list can be found at 

Regular Expression Examples  /$for.*/i  Matches any string that starts with "for" regardless of letter case  / /  matches the longest substring that starts with ' ' (because of greedy match)  / / or / /U or / ]+>/  matches any HTML tag in a string  A '?' after a quantifier indicates "non-greedy match" is to be used.

PHP Functions and Examples // Check if a user's name is made up of 8-12 // alphanumeric characters if (preg_match('/^[A-Za-z0-9]{8,12}$/', $username)) { // OK } // The pattern /a[\S]*/ matches a substring that starts with // 'a' and follows by any number of non-white space characters. if (preg_match('/a[\S]*/', "x abc faa axx", $match) { // $match is the first substring that matches the pattern echo $match; // Output abc } if (preg_match_all( '/a[\S]*/', "x abc faa axx", $matches) { // $matches becomes an array containing "abc", "aa", and "axx" }

PHP Functions and Examples $str = preg_replace('/dog/', 'cat', 'I love dog.'); // $str becomes "I love cat."; // Using backreferences followed by numeric literals $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}1,$3'; $out = preg_replace($pattern, $replacement, $string); // $out becomes "April1,2003" // Strips excess whitespace from a string. $str = 'foo o'; $str = preg_replace('/\s\s+/', ' ', $str); // $str becomes "foo o" // S plit the phrase by any number of commas or white space characters $keywords = preg_split("/[\s,]+/", "foo, bar, baz"); // keywords becomes an array containing "foo", "bar", "baz"

References and Related Resources  Regular Expressions (Perl-Compatible)   PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition), by Larry Ullman. Peachpit Press, 2007 (Chapter 13, pp )