Exercises from the Book 'PHP and MySQL for Dynamic Web Sites' by Larry Ullman
Submitted by: Mike Murphy, Aug 2013

Chapter 14 Completed Exercises

Chapter 14 Review

Question Answer
1A. What function is used to match a regular expression?

1B. What function is used to find all matches of a regular expression?

1C. What function is used to replace matches of a regular expression?
The preg_match() function searches strings for pattern, returning true if pattern exists, and false otherwise.
preg_match ( pattern, subject [, matches, flags, offset]);

The preg_match_all() function matches all occurrences of pattern in strings, and returns the number of matches.
preg_match_all ( pattern, subject [, matches, flags, offset]);

The preg_replace() function operates just like POSIX function ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters. The modified string is returned.
preg_replace ( pattern, replacement, subject [, limit, count]);
2. What characters can you use and not use to delineate a regular expression? You can use nearly any non-alphanumeric character. The backslash and alphanumeric characters cannot be used.
3. How do you match a literal character or string of characters? In PHP patterns, characters that match themselves are called literals; characters that have special meanings are called meta-characters. A series of meta-characters enables you to validate, or match values of entire strings, beginning, and ending characters.

Also, you can match an exact string to another with the strstr() function.
4. What are meta-characters? How do you escape a meta-character? These are special symbols that change the meaning of the literal pattern characters. The dot (.) meta-character matches any single character except newline (\). The vertical pipe (|) meta-character is used for alternatives in a regular expression. A character class, enclosed in square brackets, lets you represent a bunch of characters as a single item in a regular expression. The meta-characters +, *, ?, and {} affect the number of times a pattern should be matched.

Escape a meta-character with a backslash.
5. What meta-character do you use to bind a pattern to the beginning of a string? To the end? A dollar sign ($) is used to match strings that end with the given pattern. A caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string.
6. How do you create subpatterns? Subpatterns are delimited by parentheses, which can be nested. Marking part of a pattern as a subpattern does two things: It localizes a set of alternatives, and it sets up the subpattern as a capturing subpattern