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.

Slides:



Advertisements
Similar presentations
LIS651 lecture 4 regular expressions Thomas Krichel
Advertisements

Perl & Regular Expressions (RegEx)
FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
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.
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.
1 CO3041 Databases and the Web Week 8 Dr James Denholm-Price.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
Regular expressions (contd.) -- remembering subpattern matches When a is being matched with a target string, substrings that match sub-patterns can be.
Regular Expressions. What are regular expressions? A means of searching, matching, and replacing substrings within strings. Very powerful (Potentially)
Scripting Languages Chapter 8 More About Regular Expressions.
LIS651 lecture 4 regular expressions Thomas Krichel
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.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Last Updated March 2006 Slide 1 Regular Expressions.
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’
Regular Expressions in.NET Ashraya R. Mathur CS NET Security.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
Perl 6 Update - PGE and Pugs Dr. Patrick R. Michaud April 26, 2005.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expression in Java 101 COMP204 Source: Sun tutorial, …
Programming in Perl regular expressions and m,s operators Peter Verhás January 2002.
Regular Expressions.
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.
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.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
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
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
Satisfy Your Technical Curiosity Regular Expressions Roy Osherove Methodology & Team System Expert Sela Group The.
JavaScript, Part 2 Instructor: Charles Moen CSCI/CINF 4230.
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,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
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.
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.
1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong. Adminstrivia Homework 4 not yet graded …
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
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.
Martin Kruliš by Martin Kruliš (v1.1)1.
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.
Regular Expressions Copyright Doug Maxwell (
Looking for Patterns - Finding them with Regular Expressions
Regular Expressions in Perl
Regular Expressions and perl
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 ©
CSCI 431 Programming Languages Fall 2003
Regular Expressions and Grep
CIT 383: Administrative Scripting
PHP –Regular Expressions
LIS651 lecture 4 regular expressions
Presentation transcript:

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 library (Perl- Compatible Regular Expressions)PCRE library (Perl- Compatible Regular Expressions The oldest set of regex functions are those that start with ereg. They implement POSIX Extended Regular Expressions. Portable Operating System Interface [for Unix] POSIX Extended Regular Expressions

PCRE library (Perl-Compatible Regular Expressions When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non- backslash, non-whitespace character. Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns. –/foo bar/ –#^[^0-9]$# –+php+ –%[a-zA-Z0-9_-]%

Delimiters If the delimiter needs to be matched inside the pattern it must be escaped using a backslash. If the delimiter appears often inside the pattern, it is a good idea to choose another delimiter in order to increase readability. –/ – #

Meta-characters The power of regular expressions comes from the ability to include alternatives and repetitions in the pattern. These are encoded in the pattern by the use of meta- characters. Some of the meta chars are.. –\ general escape character with several uses –^ assert start of subject (or line, in multiline mode) –$ assert end of subject (or line, in multiline mode) –. match any character except newline (by default) –[ start character class Definition

Meta Chars contd.. ] –end character class definition | –start of alternative branch ( –start subpattern ) –end subpattern ? –extends the meaning of (, also 0 or 1 quantifier, also makes greedy quantifiers lazy * –0 or more quantifier + –1 or more quantifier { –start min/max quantifier

} –end min/max quantifier Part of a pattern that is in square brackets is called a "character class". In a character class the only meta- characters are: \ –general escape character ^ –negate the class, but only if the first character - –indicates character range ] –terminates the character class

Escape Sequences The backslash character has several uses. Firstly, if it is followed by a non- alphanumeric character, it takes away any special meaning that character may have. –This use of backslash as an escape character applies both inside and outside character classes. –For example, if you want to match a "*" character, you write "\*" in the pattern.

A second use of backslash provides a way of encoding non-printing characters in patterns in a visible manner \a –alarm, that is, the BEL character (hex 07) \f –formfeed (hex 0C) \n –newline (hex 0A) \t –tab (hex 09)

PCRE Functions preg_filter — Perform a regular expression search and replacepreg_filter preg_grep — Return array entries that match the patternpreg_grep preg_last_error — Returns the error code of the last PCRE regex executionpreg_last_error preg_match_all — Perform a global regular expression matchpreg_match_all preg_match — Perform a regular expression matchpreg_match preg_quote — Quote regular expression characterspreg_quote preg_replace_callback — Perform a regular expression search and replace using a callbackpreg_replace_callback preg_replace — Perform a regular expression search and replacepreg_replace preg_split — Split string by a regular expressionpreg_split

POSIX Regex Functions ereg_replace — Replace regular expressionereg_replace ereg — Regular expression matchereg eregi_replace — Replace regular expression case insensitiveeregi_replace eregi — Case insensitive regular expression matcheregi split — Split string into array by regular expressionsplit spliti — Split string into array by regular expression case insensitivespliti sql_regcase — Make regular expression for case insensitive matchsql_regcase

ereg (PHP 4, PHP 5) ereg — Regular expression match Syntax –int ereg ( string $pattern, string $string [, array &$regs ] ) Searches a string for matches to the regular expression given in pattern in a case-sensitive way. –pattern Case sensitive regular expression. –string The input string. –regs If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs, the matches will be stored in the elements of the array regs. $regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched.

Example code snippet takes a date in ISO format (YYYY- MM-DD) and prints it in DD.MM.YYYY format:

eregi (PHP 4, PHP 5) eregi — Case insensitive regular expression match Syntax –int eregi ( string $pattern, string $string [, array &$regs ] ) This function is identical to ereg() except that it ignores case distinction when matching alphabetic characters.ereg() Example –

Differences from POSIX regex As of PHP 5.3.0, the POSIX Regex extension is deprecated.POSIX Regex There are a number of differences between POSIX regex and PCRE regex –The PCRE functions require that the pattern is enclosed by delimiters.delimiters –Unlike POSIX, the PCRE extension does not have dedicated functions for case-insensitive matching. Instead, this is supported using the /i pattern modifierpattern modifier –The POSIX functions find the longest of the leftmost match, but PCRE stops on the first valid match.

Function Replacements

References PCRE Functions – POSIX Regex Functions – String Functions –