Download presentation
Presentation is loading. Please wait.
Published byMarybeth Stephens Modified over 9 years ago
1
Chapter 14 Perl-Compatible Regular Expressions Part 1
2
preg_match The match function is int preg_match(string $pattern, string $str ) – 1 st argument is the pattern – 2 nd argument is the subject string – Returns number of matches found: 1 (true) if there is a match – it stops at the first match found! 0 (false) otherwise
3
pcre.php Script 14.1 on page 435 http://cscdb.nku.edu/csc301/frank/ch14/pcre. php http://cscdb.nku.edu/csc301/frank/ch14/pcre. php ch14\pcre.php
4
Character Classes [A-Z]uppercase letter [a-z]lowercase letter [0-9]digit {5}exactly five {3,}three or more {1,4}one to four 4
5
Metacharacters ^beginning of string $end of string |or 5
6
Regular Expression Username – 5 to 10 lowercase letters? $pattern = '/^[a-z]{5,10}$/'; if (!preg_match($pattern, $username)) { echo " $username is invalid. "; } 6
7
Regular Expressions Social Security Number 123-45-6789 $pattern = '/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/'; if (!preg_match($pattern, $ssn)) { echo " $ssn is invalid. "; } 7
8
Character classes \dany digit [0-9] \Dany non-digit [^0-9] \wAny letter, number, or underscore [A-Za-z0-9_] \WAnything that is not a letter, number, or underscore [^A-Za-z0-9_] 8
9
Character classes \swhitespace (space, tab, new line, etc.) [\f\r\t\n\v] \SAny non-whitespace [^\f\r\t\n\v] 9
10
Regular Expressions Social Security Number 123-45-6789 $pattern = '/^\d{3}-\d{2}-\d{4}$/'; if (!preg_match($pattern, $ssn)) { echo " $ssn is invalid. "; } 10
11
Metacharacters [0-9]** = zero or more [A-Z]++ = one or more.. = match any character ([A-Z]\. )?? = either zero or one 11
12
Regular Expressions Last name: Frank $pattern = '/^[A-Z][a-z]+$/'; if (!preg_match($pattern, $last)) { echo " $last is invalid. "; } 12
13
Regular Expressions Name: Charles Frank or Charles E. Frank $pattern = '/^[A-Z][a-z]+ ([A-Z]\. )?[A-Z][a-z]+ $/'; if (!preg_match($pattern, $name)) { echo " $name is invalid. "; } 13
14
Zip Code Page 444 /^(\d{5}(-\d{4})?$/
15
Email Address Page 445 /^[\w.]+@[\w.-]+\.[A-Za-z]{2,6}$/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.