Regular Expressions

Introduction to
PHP and MySQL
Kirkwood Center for
Continuing Education
By Fred McClurg, frmcclurg@gmail.com
Copyright © 2010 All Rights Reserved.
1
Chapter Sixteen
Regular Expressions
http://webcert.kirkwood.edu/~fmcclurg
/courses/php/slides/chapter16.ppt
2
Regular Expressions
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 Expression Quantifiers
Description: Quantifiers are wildcards that specify how
many times to match the preceding string pattern.
Quantifier
*
+
?
{num}
{min,}
{min,max}
Meaning
Match zero or more of previous
Match one or more of previous
Match one or zero of previous
Match exactly num of previous
Match at least min of previous
Match at least min but not more
than max of previous characters
Alternate
Notation
{0,}
{1,}
{0,1}
4
Regular Expression Anchors
Description: Anchors define a specific location for the
match to take place.
Anchor
^
$
.
\b
\B
Meaning
Match start of line
Match end of line
Match any single character
Match word boundary
Match non-word boundary
5
Character Classes
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
Negated Character Classes
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
Alternatives
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
Subpatterns
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
Greedy Quantifiers
Greedy Defined: By default, regular expressions are
“greedy” meaning that '.*' and '.+' will always
match as the longest string.
Example:
Given the string:
do <b>not</b> press
And the regular expression:
<.*>
The resulting match would be:
<b>not</b>
10
Non-greedy Quantifiers
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 <b>not</b> press
And the regular expression:
<.*?>
The resulting match would be:
<b>
11
Perl Style Character Classes
Defined: A method of matching a set of characters.
Symbol
\s
\S
\w
\W
\d
\D
Meaning
Match whitespace
Match non-whitespace
Match word character
Match non-word character
Match a numeric digit
Match a non-numeric digit
Alternate Notation
[\r\n \t]
[^\r\n \t]
[A-Za-z0-9_]
[^A-Za-z0-9_]
[0-9]
[^0-9]
12
Regular Expression Modifiers
Description: Modifiers change the matching
behavior of the regular expression.
Modifier
i
s
m
Meaning
Ignore case (case insensitive)
Dot matches all characters
Match start and end of line anchors at
embedded new lines in search string
Example match upper and lower case “abc”:
'/abc/i'
13
Regular Expressions in PHP
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
Using “preg_match()”
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." );
}
?>
15
Using “preg_match_all()”
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 );
?>
16
to be continued ...
http://webcert.kirkwood.edu/~fmccl
urg/courses/php/slides/recipe01.ppt