Statement
A word boundary is defined as the point between a whitespace character and an identifier (alphanumeric or underscore) character:
preg_match(“/[[:<:]]gun[[:>:]]/”, “the Burgundy exploded”); // returns false
preg_match(“/gun/”, “the Burgundy exploded”); // returns true
Note that the beginning and end of a string also qualify as word boundaries.
test
Quantifiers and GreedRegular expression quantifiers are typically greedy. That is, when faced with a quantifier, the engine matches as much as it can while still satisfying the rest of the pattern. For instance:
preg_match(“/(<.*>)/”, “do not press the button”, $match);
// $match[1] is ‘not‘
The regular expression matches from the first less-than sign to the last greater-than sign. In effect, the .* matches everything after the first less-than sign, and the engine backtracks to make it match less and less until finally there’s a greater-than sign to be matched.
This greediness can be a problem. Sometimes you need minimal (nongreedy) matching—that is, quantifiers that match as few times as possible to satisfy the rest of the pattern. Perl provides a parallel set of quantifiers that match minimally. They’re easy to remember, because they’re the same as the greedy quantifiers, but with a question mark (?) appended. Table 4-9 shows the corresponding greedy and nongreedy quantifiers supported by Perl-style regular expressions.
Table 4-. Greedy and nongreedy quantifiers in Perl-compatible regular expressions
Greedy quantifier
Nongreedy quantifier
?
??
*
*?
+
+?
{m}
{m}?
{m,}
{m,}?
{m,n}
{m,n}?
Here’s how to match a tag using a nongreedy quantifier:
preg_match(“/(<.*?>)/”, “do not press the button”, $match);
// $match[1] is ““
Another, faster way is to use a character class to match every non-greater-than character up to the next greater-than sign:
preg_match(“/(<[^>]*>)/”, “do not press the button”, $match);
// $match[1] is ”
Noncapturing Groups
If you enclose a part of a pattern in parentheses, the text that matches that subpattern is captured and can be accessed later. Sometimes, though, you want to create a subpattern without capturing the matching text. In Perl-compatible regular expressions, you can do this using the (?: subpattern ) construct:
preg_match(“/(?:ello)(.*)/”, “jello biafra”, $match);
// $match[1] is ” biafra”
Backreferences
You can refer to text captured earlier in a pattern with a backreference: 1 refers to the contents of the first subpattern, 2 refers to the second, and so on. If you nest subpatterns, the first begins with the first opening parenthesis, the second begins with the second opening parenthesis, and so on.
For instance, this identifies doubled words:
preg_match(“/([[:alpha:]]+)s+1/”, “Paris in the the spring”, $m);
// returns true and $m[1] is “the”
The preg_match() function captures at most 99 subpatterns; subpatterns after the 99th are ignored.
Trailing Options
Perl-style regular expressions let you put single-letter options (flags) after the regular expression pattern to modify the interpretation, or behavior, of the match. For instance, to match case-insensitively, simply use the i flag:
preg_match(“/cat/i”, “Stop, Catherine!”); // returns true
Table 4-10 shows the modifiers from Perl that are supported in Perl-compatible regular expressions.
Table 4-. Perl flags
Modifier
Meaning
/regexp/i
Match case-insensitively
/regexp/s
Make period (.) match any character, including newline (n)
/regexp/x
Remove whitespace and comments from the pattern
/regexp/m
Make caret (^) match after, and dollar sign ($) match before, internal newlines (n)
/regexp/e
If the replacement string is PHP code, eval() it to get the actual replacement string
PHP’s Perl-compatible regular expression functions also support other modifiers that aren’t supported by Perl, as listed in Table 4-11.
Table 4-. Additional PHP flags
Modifier
Meaning
/regexp/U
Reverses the greediness of the subpattern; * and + now match as little as possible, instead of as much as possible
/regexp/u
Causes pattern strings to be treated as UTF-8
/regexp/X
Causes a backslash followed by a character with no special meaning to emit an error
/regexp/A
Causes the beginning of the string to be anchored as if the first character of the pattern were ^
/regexp/D
Causes the $ character to match only at the end of a line
/regexp/S
Causes the expression parser to more carefully examine the structure of the pattern, so it may run slightly faster the next time (such as in a loop)
It’s possible to use more than one option in a single pattern, as demonstrated in the following example:
$message = <<< END
To: you@youcorp
From: me@mecorp
Subject: pay up
Pay me or else!
END;
preg_match(“/^subject: (.*)/im”, $message, $match);
print_r($match);
pay up
Inline Options