Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1."— Presentation transcript:

1 Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com Copyright © 2010 All Rights Reserved. 1

2 Chapter Sixteen Regular Expressions http://webcert.kirkwood.edu/~fmcclurg /courses/php/slides/chapter16.ppt 2

3 Defined: A method of specifying a search string using a number of special characters that can precisely match a substring. Purpose: Regular expressions allow you to perform complex pattern matching on strings. A small regular expression can replace the need for a large amount of code Example for e-mail validation: ^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,4}$ Regular Expressions 3

4 Description: Quantifiers are wildcards that specify how many times to match the preceding string pattern. Regular Expression Quantifiers QuantifierMeaning Alternate Notation * Match zero or more of previous {0,} + Match one or more of previous {1,} ? Match one or zero of previous {0,1} {num} Match exactly num of previous {min,} Match at least min of previous {min,max} Match at least min but not more than max of previous characters 4

5 Description: Anchors define a specific location for the match to take place. Regular Expression Anchors AnchorMeaning ^ Match start of line $ Match end of line. Match any single character \b Match word boundary \B Match non-word boundary 5

6 Defined: A method of matching a set of characters. Examples Match any upper or lower case alpha character: [A-Za-z] Match any lower case vowel character: [aeiou] Match any numeric digit character: [0-9] Character Classes 6

7 Defined: A method of matching characters that are outside the specified set. Examples Match any non-alpha character: [^A-Za-z] Match any non-vowel character: [^aeiou] Match any non-numeric digit: [^0-9] Match any character outside the ASCII range (e.g. non-printable characters like tab, linefeed, CTRL, etc.): [^ -~] Negated Character Classes 7

8 Defined: The vertical pipe character allows you to match one string or another. Example: Match the string “red”, “white”, or “blue”: Regular Expression: red|white|blue Alternatives 8

9 Defined: Parentheses can be used to group regular expressions. Example: Match any of the following: a long time ago a long long time ago a long long long time ago Regular Expression: (long )+ Subpatterns 9

10 Greedy Defined: By default, regular expressions are “greedy” meaning that '.* ' and '.+ ' will always match as the longest string. Example: Given the string: do not press And the regular expression: The resulting match would be: not Greedy Quantifiers 10

11 Non-greedy Defined: Non-greedy quantifiers specify minimal string matching. A question mark “ ? ” placed after the wildcard, cancels the “greedy” regular expression behavior. Example: Given the string: do not press And the regular expression: The resulting match would be: Non-greedy Quantifiers 11

12 Defined: A method of matching a set of characters. Perl Style Character Classes SymbolMeaning Alternate Notation \s Match whitespace [\r\n \t] \S Match non-whitespace [^\r\n \t] \w Match word character [A-Za-z0-9_] \W Match non-word character [^A-Za-z0-9_] \d Match a numeric digit [0-9] \D Match a non-numeric digit [^0-9] 12

13 Description: Modifiers change the matching behavior of the regular expression. Example match upper and lower case “abc”: '/abc/i' Regular Expression Modifiers ModifierMeaning i Ignore case (case insensitive) s Dot matches all characters m Match start and end of line anchors at embedded new lines in search string 13

14 Discussion: PHP functions that use Perl regular expressions start with “preg _ ”. The regular expressions are in Perl format meaning, they start and end with a slash. In order to prevent variable expansion, regular expressions are usually specified with single quotes. Regular Expressions in PHP 14

15 Discussion: The function “preg_match()” returns the number of times a pattern matches a string. Because it returns zero if there are no matches, this function works ideal in an “if” statement. Example: <?php $subject = "Jack and Jill went up the hill."; $pattern = '/jack|jill/i'; if ( preg_match( $pattern, $subject ) ) { printf( "Regular expression matched." ); } ?> Using “preg_match()” 15

16 Discussion: The function “preg_match_all()” returns the number of times a pattern matches a string. Because it returns zero if there are no matches, this function works ideal in an “if” statement. Example: <?php $subject = "Every good boy deserves fudge."; $pattern = '/[aeiou]/i'; preg_match_all( $pattern, $subject, $matches ); $total = count( $matches[0] ); // matches array printf( "String contains %d vowels", $total ); ?> Using “preg_match_all()” 16

17 to be continued... http://webcert.kirkwood.edu/~fmccl urg/courses/php/slides/recipe01.ppt


Download ppt "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1."

Similar presentations


Ads by Google