Download presentation
Presentation is loading. Please wait.
Published byKelley Hopkins Modified over 8 years ago
1
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com Copyright © 2015, Fred McClurg, All Rights Reserved.
2
2 Regular Expressions http://cecert.kirkwood.edu/~fmcclurg/courses /php/slides/chapter17b.regex.ppt
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}$ 3 Regular Expressions
4
Description: Quantifiers are wildcards that specify how many times to match the preceding string pattern. 4 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
5
Description: Anchors define a specific location for the match to take place. 5 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
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] 6 Character Classes
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.): [^ -~] 7 Negated Character Classes
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 8 Alternatives
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 )+ 9 Subpatterns
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 10 Greedy Quantifiers
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: 11 Non-greedy Quantifiers
12
Defined: A method of matching a set of characters. 12 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]
13
Example match upper and lower case “abc”: '/abc/i' 13 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 Description: Modifiers change the matching behavior of the regular expression.
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. 14 Regular Expressions in PHP
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."; $subject = "Jack and Jill went up the hill."; $pattern = '/jack|jill/i'; $pattern = '/jack|jill/i'; if ( preg_match( $pattern, $subject ) ) if ( preg_match( $pattern, $subject ) ) { printf( "Regular expression matched." ); printf( "Regular expression matched." ); }?> 15 Using “preg_match()”
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."; $subject = "Every good boy deserves fudge."; $pattern = '/[aeiou]/i'; $pattern = '/[aeiou]/i'; preg_match_all( $pattern, $subject, $matches ); preg_match_all( $pattern, $subject, $matches ); $total = count( $matches[0] ); // matches array $total = count( $matches[0] ); // matches array printf( "String contains %d vowels", printf( "String contains %d vowels", $total ); $total );?> 16 Using “preg_match_all()”
17
to be continued... http://cecert.kirkwood.edu/~fmcclurg/cou rses/php/slides/recipe01.ppt http://cecert.kirkwood.edu/~fmcclurg/cou rses/php/slides/recipe01.ppt 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.