05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

pattern. Perl provides a parallel set of quantifiers that match minimally. They’re easy<br />

to remember, because they’re the same as the greedy quantifiers, but with a question<br />

mark (?) appended. Table 4-11 shows the corresponding greedy and non-greedy<br />

quantifiers supported by Perl-style regular expressions.<br />

Table 4-11. Greedy and non-greedy quantifiers in Perl-compatible regular expressions<br />

Greedy quantifier Non-greedy quantifier<br />

? ??<br />

* *?<br />

+ +?<br />

{m} {m}?<br />

{m,} {m,}?<br />

{m,n} {m,n}?<br />

Here’s how to match a tag using a non-greedy quantifier:<br />

preg_match('/()/', 'do not press the button', $match);<br />

// $match[1] is ''<br />

Another, faster way is to use a character class to match every non-greater-than character<br />

up to the next greater-than sign:<br />

preg_match('/(]*>)/', 'do not press the button', $match);<br />

// $match[1] is ''<br />

Non-Capturing Groups<br />

If you enclose a part of a pattern in parentheses, the text that matches that subpattern<br />

is captured and can be accessed later. Sometimes, though, you want to create a<br />

subpattern without capturing the matching text. In Perl-compatible regular expressions,<br />

you can do this using the (?:subpattern) construct:<br />

preg_match('/(?:ello)(.*)/', 'jello biafra', $match);<br />

// $match[1] is ' biafra'<br />

Backreferences<br />

You can refer to text captured earlier in a pattern with a backreference: \1 refers to<br />

the contents of the first subpattern, \2 refers to the second, and so on. If you nest<br />

subpatterns, the first begins with the first opening parenthesis, the second begins<br />

with the second opening parenthesis, and so on.<br />

For instance, this identifies doubled words:<br />

preg_match('/([[:alpha:]]+)\s+\1/', 'Paris in the the spring', $m);<br />

// returns true and $m[1] is 'the'<br />

You can’t capture more than 99 subpatterns.<br />

106 | Chapter 4: Strings<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!