Specifications

Counted Subexpressions
We can specify how many times something can be repeated by using a numerical expression in
curly braces ( {} ).You can show an exact number of repetitions ({3} means exactly 3 repeti-
tions), a range of repetitions ({2, 4} means from 2 to 4 repetitions), or an open ended range of
repetitions ({2,} means at least two repetitions).
For example,
(very ){1, 3}
matches “very”, “very very” and “very very very”.
Anchoring to the Beginning or End of a String
You can specify whether a particular subexpression should appear at the start, the end, or both.
This is pretty useful when you want to make sure that only your search term and nothing else
appears in the string.
The caret symbol (^) is used at the start of a regular expression to show that it must appear at
the beginning of a searched string, and $ is used at the end of a regular expression to show that
it must appear at the end.
For example, this matches bob at the start of a string:
^bob
This matches com at the end of a string:
com$
Finally, this matches any single character from a to z, in the string on its own:
^[a-z]$
Branching
You can represent a choice in a regular expression with a vertical pipe. For example, if we
want to match com, edu, or net, we can use the expression:
(com)|(edu)|(net)
Matching Literal Special Characters
If you want to match one of the special characters mentioned in this section, such as ., {, or $,
you must put a slash (\) in front of it. If you want to represent a slash, you must replace it with
two slashes, \\.
Using PHP
P
ART I
112
06 7842 CH04 3/6/01 3:41 PM Page 112