Think of regular expressions as an elaborate system of matching patterns. You
first write the pattern, then use one of PHP's built-in functions to apply the
pattern to a text string regular expressions are specifically for use with
strings. PHP has essentially two functions for using regular expressions to
match patterns one case sensitive and one not and two for matching patterns
and replacing matched text with other text again, one case sensitive and one
not
Some text editors, such as BBEdit for Macintosh, TextPad for
Windows and emacs for Unix, allow you to use regular expressions to match and
replace patterns within and throughout several documents.
This may be another good reason to learn
regular expressions and is perhaps something to consider when choosing your text
editor.
Special Characters for Regular Expression | |
---|---|
Character | Matches |
. | any character |
^a | begins with a |
a$ | ends with a |
a+ | at least one a |
a? | zero or one a |
\n | new line |
\t | tab |
\ | escape |
(ab) | ab grouped |
a|b | a or b |
a{ 2} | aa |
a{ 1,} | a, aa, aaa, etc. |
a{ 1,3} | a, aa, aaa |
[a-z] | any lowercase letter |
[A-Z] | any uppercase letter |
[0-9] | any digit |
Regular expressions also make use of the pipe | as the
equivalent of or. Therefore, "a|b" will match the
strings a or b and
"gre|ay" matches both potential spellings of the color. Using the pipe within
patterns is called alternation.
Practically, of course, there's little use to matching
repetitions of a letter in a string, but these examples are good ways to
demonstrate how a symbol works. You should begin by focusing on understanding
what the various symbols mean and how they are used.
To include special characters (^.[]$()|*?{ } \) in a pattern,
they need to be escaped (a backslash put before them). This is true for the
metacharacters and the grouping symbols (parenthesis and brackets). You can also
use the backslash to match new lines ("\n") and tabs ("\t"), essentially
creating a metacharacter out of a literal.
There are two functions built in to PHP expressly for the
purpose of matching a pattern within a string: ereg() and
eregi(). The only difference between the two is that ereg()
treats patterns as case-sensitive whereas eregi() is case-insensitive,
making it less particular. The latter is generally recommend for common use,
unless you need to be more explicit (perhaps for security purposes, as with
passwords). Both functions will be evaluated to TRUE if the pattern is matched,
FALSE if it is not. Here are two different ways to use these functions:
ereg("pattern", "string");
Or:
$Pattern = "pattern"; $String = "string"; eregi($Pattern, $String);
Throughout the rest of the chapter, I will assign the pattern
to a variable, as in the second example above, to draw more attention to the
pattern itself—the heart of any regular expression.