PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.

Slides:



Advertisements
Similar presentations
FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
Advertisements

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 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Regular Expressions Comp 2400: Fall 2008 Prof. Chris GauthierDickey.
Scripting Languages Chapter 8 More About Regular Expressions.
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
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.
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
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.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
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’
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.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Finding the needle(s) in the textual haystack
Programming Languages Meeting 13 December 2/3, 2014.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expressions.
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)
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.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
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.
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,
Powerpoint Templates Page 1 Powerpoint Templates GROUP 8:REGULAR EXPRESSION GURU BESAR: PN. SARINA SULAIMAN CIKGU-CIKGU: 1.CIKGU NENI 2.CIKGU
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.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
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.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 5 Regular Expressions.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
-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.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
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.
Looking for Patterns - Finding them with Regular Expressions
Lecture 19 Strings and Regular Expressions
CSC 594 Topics in AI – Natural Language Processing
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 ©
CSC 594 Topics in AI – Natural Language Processing
Data Manipulation & Regex
Regular Expressions and Grep
PHP PART 2.
Presentation transcript:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 2 Today’s Topics Introduction Functions Syntax Examples

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 3 Try out … Open [Windows Explorer]. Point to [Program Files] folder and Right-click on it Select [Search] and type tw*.* in [All or part of the file name] and press [Enter] What do we get ????

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 4 Introduction A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is.*\.txt

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 5 Introduction Regular expression is a formatted pattern that can be used to find instances of a string in another. PHP supports two type of regular expression: POSIX Perl-compatible

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 6 Basic Syntax of Regular Expressions Three special symbols (metacharacters): –'^' the start of a string –'$' the end of a string –'.' any single character of a string

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 7 Basic Syntax of Regular Expressions "^The": –matches any string that starts with "The"; "of despair$": –matches a string that ends in the substring "of despair"; "^abc$": –a string that starts and ends with "abc" -- that could only be "abc" itself! "notice": –a string that has the text "notice" in it.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 8 Quantifiers the quantifier symbols '*', '+', and '?‘ –denote the number of times a character or a sequence of characters may occur. * "zero or more" + "one or more" ? "zero or one."

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 9 Quantifiers examples "ab* " –matches a string that has an a followed by zero or more b's –e.g. "a", "ab", "abbb " etc "ab+ " –matches a string that has an a followed by at least one b –E.g. "ab", "abbb" etc

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 10 Quantifiers examples "ab? " –there might be a b or not –E.g. "a", "ab", "abbb", "ac" "a?b+$ " –might be an a or not, followed by one or more b's ending a string. –E.g. "ab", "cab", "abbb", "cbbb"

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 11 Quantifiers (bounding) can also use bounds, which come inside braces and indicate ranges in the number of occurrences "ab{2}" –matches a string that has an a followed by exactly two b's –e.g. "abb"

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 12 Quantifiers (bounding) "ab{2,}" –there are at least two b's –e.g. "abb", "abbbb", etc. "ab{3,5}" –from three to five b's –e.g. "abbb", "abbbb", or "abbbbb"

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 13 Quantifiers (bounding) Note that you must always specify the first number of a range (i.e, "{0,2}", not "{,2}"). Also, as you might have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds "{0,}", "{1,}", and "{0,1} " respectively.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 14 Quantify a sequence of characters To quantify a sequence of characters, put them inside parentheses "a(bc)*" –matches a string that has an a followed by zero or more copies of the sequence "bc" "a(bc){1,5}" –one through five copies of "bc."

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 15 The ' | ' symbol Works as an OR operator "hi|hello" –matches a string that has either "hi" or "hello" in it "(b|cd)ef" –a string that has either "bef" or "cdef“ "(a|b)*c" –a string that has a sequence of alternating a's and b's ending in a c

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 16 The '. ' symbol (period) Stands for any single character "a.[0-9]" –matches a string that has an a followed by one character and a digit –E.g. "ae1" –bracket expressions specify which characters are allowed in a single position of a string: "^.{3}$" –a string with exactly 3 characters.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 17 PHP Regular Expression Functions POSIX Regular Expression functions: ereg - searches a string for matches of a pattern in a case-sensitive way ereg_replace - searches a string of a pattern, then replaces the matched text with a new string. split - splits a string into array by regular expression. eregi – performs the same as ereg, but this ignores case distinction eregi_replace - performs the same as ereg_replace, but this ignores case distinction spliti - performs the same as split, but this ignores case distinction.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 18 if (ereg(“Wo”,”Hello World”)) print(“The pattern is found”); //Ouput: The pattern is found int ereg ( string pattern, string string [, array &matches] ) Returns: true if pattern is found, false if pattern is not found Example: Syntax:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 19 Actual return value (php manual) Returns the length of the matched string if a match for pattern was found in string, or FALSE if no matches were found or an error occurred. If the optional parameter $matches was not passed or the length of the matched string is 0, this function returns 1.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 20 $s1=“Hello World”; $s2 = ereg_replace(“l”, “L”, $s1); //result: $s2=“HeLLo WorLd”. $s1 remains unchanged string ereg_replace ( string pattern, string replacement, string string) Returns: if pattern is found, it returns the replaced string. Otherwise, it returns the original string Example: Syntax:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 21 $a=“Hello World again”; $b = split(“ ”,$a); //result: $b[0]=“Hello” // $b[1]=“World” // $b[2]=“again” // $a remains unchanged array split ( string pattern, string string [, int limit]) Example 1: Syntax: $a=“Hello World again”; $b = split(“ ”,$a, 2); //result: $b[0]=“Hello” // $b[1]=“World again” // $a remains unchanged Example 2:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 22 Regular Expression Syntax Metacharacters: ^ (caret) - searches at the beginning of the string $ (dollar sign) - searches at the end of string. (dot) - searches for any character $a1 = ereg(“^hello”,”hello world”); // $a1=true $a2 = ereg(“^hello”,”I say hello world”); // $a2=false $b1 = ereg(“bye$”,”goodbye”); // $b1=true $b2 = ereg(“bye$”,”goodbye my friend”); // $b2=false $c1 = ereg(“.”, ”hello”); // $c1=true $c2 = ereg(“.”, ””); // $c2=false Example:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 23 Quantifiers: {n} matches exactly n times {m,n} matches between m and n times inclusive {n,} matches n or more times + matches one or more times (same as {1,} ) * matches zero or more times (same as {0,} ) ? matches zero or one time (same as {0,1} )

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 24 $a1 = ereg(“t*”,”tom”); // $a1=true $a2 = ereg(“t*”,”fom”); // $a2=true $b1 = ereg(“w+”,”hello world”); // $b1=true $b2 = ereg(“w+”,”hello wwwwwwwworld”); // $b2=true $c1 = ereg(“b?”, ”book”); // $c1=true $c2 = ereg(“b?”, ”cook”); // $c2=true Example 1:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 25 $a1 = ereg("l{2}","hello world"); // $a1=true $a2 = ereg(“o{2}”,”hello world”); // $a2=false $b1 = ereg(“o{1,2}”,”hello world”); // $b1=true $b2 = ereg(“u{0,2}”,”hello world”); // $b2=true $c1 = ereg(“d{1,}”, ”hello world”); // $c1=true $c2 = ereg(“d{2,}”, ”hello world”); // $c2=false Example 2:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 26 Escape sequence characters: Use a backslah, \ before the character \^ caret character \$ dollar sign \( opening parenthesis \) closing parenthesis \. dot character \| or symbol \* asterisk character \? question mark \\ backslash \{ opening bracket \} closing bracket

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 27 //search for a string p^ at the beginning $a = ereg(“^p\^”,”p^2 means p power of 2”); // $a=true //search for a dollar sign at the end $b = ereg(“\$$”,”hello world$”); // $b=true //replace dot characters to a string ‘ dot ’ $c = ereg_replace(“\.”, ” dot “, ”world.com.my”); // result: $c=“world dot com dot my $d = ereg_replace(“.”, ” dot “, ”com.my”); // result: $d=“ dot dot dot dot dot dot //search for a question mark in a string $e = ereg(“\?”, ”hello“); // $e=false $f = ereg(“?”, ”hello“); // error – because what to repeat? $g = ereg(“.?”, ”hello“); // $g=true – repeat 0 or 1 time of // any character Example:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 28 Grouping patterns: Related patterns can be grouped together between square brackets [] //check whether all characters in a string are in lower case $a1 = ereg(“^[a-z]+$”,”hello”); // $a1=true $a2 = ereg(“^[a-z]+$”,”hello world”); // $a2=false, because the space //check whether all characters are in a string in upper case $b = ereg(“^[A-Z]+$”,”Hello”); // $b=false //check whether a string is a numerical string $c = ereg(“^[0-9]+$”,”123”); // $c=true Example 1:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 29 //check whether a string contains alphabets but not numbers $d1 = ereg(“^[a-zA-Z]+$”,”HeLLo”); // $d1=true $d2 = ereg(“^[a-zA-Z]+$”,”123Hello”); // $d2=false //the first character must be a number followed by alphabets $a1 = ereg(“^[0-9][a-zA-Z]+$”,”2abC”); // $a1=true $a2 = ereg(“^[0-9][a-zA-Z]+$”,”22abC”); // $a2=false $a3 = ereg(“^[0-9][a-zA-Z]+$”,”2abC2”); // $a3=false Example 2:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 30 Grouping terms: Related terms can be grouped together using parentheses () // check whether a string starts with hello or world $a1 = ereg(“^(hello|world)”,”hello world”); // $a1=true $a2 = ereg(“^(hello|world)”,”world wide web”); // $a2=true //check whether a string contains repeated uppercase alphabet and //number. eg. A2C4D1 $b1 = ereg(“^([A-Z][0-9])+$”,”A2C4D1”); // $b1=true $b2 = ereg(“^([A-Z][0-9])+$”,”AAC4D1”); // $b2=false $b3 = ereg(“^([A-Z][0-9])+$”,”A222222”); // $b3=false //if (b3 above) doesn’t use parentheses $c = ereg(“^[A-Z][0-9]+$”,”A222222”); // $c=true Example:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 31 Predefined character classes: [[:alpha:]] Alphabet characters (same as [a-zA-Z] ) [[:digit:]] Digit characters (same as [0-9] ) [[:alnum:]] Alphanumeric characters (i.e, letters [a-zA-Z] or digits [0-9]) [[:lower:]] Lowercase letters (same as [a-z] ) [[:upper:]] Uppercase letters (same as [A-Z] ) [[:space:]] Whitespaces (i.e, space, tab or newline characters) // check whether the string is a number // both regular expressions below are equivalent $a = ereg(“^[[:digit:]]$”,”5467”); // $a=true $b = ereg(“^[0-9]$”,”5467”); // $b=true Example:

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 32 Applications of Regular Expression Validating formats or patterns Pre-processing strings Counting the occurrences of pattern Filtering records

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 33 Example We’ll use the form below for the following examples. form.html

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 34 Validating Format: Example 1: Validating the IP address of accessing users Let say, students are only allowed to fill in the form from their residential college. Assume the IP address of each college follows this format: X.Y, where X = 70, 71, 75 or 80 and Y is a number. <?php // Firstly, read the IP address of the client PC from an environment variable $ip = $_SERVER['REMOTE_ADDR']; // then checks whether the ip address follows the format. // If it does not, terminate the page if (!ereg("161\.139\.(70|71|75|80)\.[0-9]+",$ip)) die("You are not allowed to access this page"); ?> If the IP address has been validated, this part and beyond are accessible to the user form.php

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 35 Validating Format: Example 2: Validating the user’s Name Let say, the Name must contain at least three characters and at least two words. <?php // Firstly, read the Name $Name = $_GET[‘Name']; // check whether the Name contains at least 3 characters if (!ereg(".{3,}",$Name)) print(“You must enter at least 3 characters"); // check whether the Name contains at least 2 words if (!ereg("[[:alpha:]]+[[:space:]]([[:alpha:]]+[[:space:]]*)+",$Name)) print(“You must enter at least 2 words"); ?> form.php

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 36 Validating Format: Example 3: Validating address eg valid address: eg invalid address: utm.my <?php // Firstly, read the address $ = $_GET[‘ ']; // check whether the address is valid if print(“Invalid address"); ?> form.php

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 37 Validating Format: Example 4: Validating Handphone numbers Let say the valid mobile phone numbers are in this format: 01X where 01X is 012, 013, 016, 017 or 019 <?php // Firstly, read the Handphone number $Handphone = $_GET[‘Handphone']; // check whether the Handphone number is valid if (!ereg("01(0|2|3|6|7|8|9)-[0-9]{7}",$Handphone)) print(“Invalid Phone number"); ?> form.php

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 38 Pre-processing String: Example: In the following example, the program reads the Address which is entered by a user, then removes the new line characters and lastly removes the redundant spaces. Let say the input is as the figure below. The program will receive the Address as “No. 32, Kolej 12, \n Universiti Teknologi Malaysia \n Skudai, Johor” Sometimes we need to process a string before we use it.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 39 Pre-processing String: <?php // Firstly, read the Address $Address = $_GET[‘Address']; // Let say the $Address is //“No. 32, Kolej 12, \n Universiti Teknologi Malaysia \n Skudai, Johor” // Replacing newline characters with spaces $Address = ereg_replace("\n", ” ”, $Address); // The $Address becomes //“No. 32, Kolej 12, Universiti Teknologi Malaysia Skudai, Johor” // Removing redundant spaces $Address = ereg_replace("[[:space:]]{2,}", ” ”, $Address); // The $Address becomes //“No. 32, Kolej 12, Universiti Teknologi Malaysia Skudai, Johor” ?> form.php

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 40 Counting the number of occurrences: <?php // Let say the string is $s = “There is no counting function in regular expression.“. “To count the number of occurrences of a pattern in “. “a string, we can use functions split() and count(). “; // Counting the number of words $words = split("[[:space:]]",$s); print ("Number of words:".count($words)); // Output: 27 // Counting the number of sentences $sentences = split("\.",$s); print ("Number of sentences:".count($sentences)); // Output: 3 ?> form.php Regular expression does not provide any function to count the number occurrences of a pattern in a string. The trick for doing this is, firstly splits the string and then counts the result.

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 41 Filtering records: Filtering means, what records should be or should not be selected. Usually, we use SQL statements for doing this. Example: Filtering records using SQL Let say we have a table named ‘person’ as below and we want to display all persons who his/her name starts with ‘A’ Table: person Expected Output

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 42 Filtering records: Name Gender <?php $conn = mysql_connect('localhost','example','abc123'); $db=mysql_select_db('db_example',$conn); $query = mysql_query("select * from person where name like 'A%'"); while ( $row=mysql_fetch_row($query) ) { $name = $row[0]; $gender = $row[1]; print(" ".$name." ".$gender." \n"); } ?> form.php Example: Using SQL statements SQL has its own regular expression. It uses the operator like

PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 43 Filtering records: Name Gender <?php $conn = mysql_connect('localhost','example','abc123'); $db=mysql_select_db('db_example',$conn); $query = mysql_query("select * from person"); while ( $row=mysql_fetch_row($query) ) { $name = $row[0]; $gender = $row[1]; if (!ereg("^A",$name)) continue; // do not display the record if it is not // matched with the pattern print(" ".$name." ".$gender." \n"); } ?> form.php Example: Filtering using PHP regular expression In this technique, the SQL statement is still needed. But it is only used for retrieving records from the database. The filtering process is done by the PHP regular expression.