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.

Character Classes<br />

Perl-style regular expressions support the POSIX character classes but also define<br />

some of their own, as shown in Table 4-9.<br />

Table 4-9. Perl-style character classes<br />

Character class Meaning Expansion<br />

\s Whitespace [\r\n \t]<br />

\S Non-whitespace [^\r\n \t]<br />

\w Word (identifier) character [0-9A-Za-z_]<br />

\W Non-word (identifier) character [^0-9A-Za-z_]<br />

\d Digit [0-9]<br />

\D Non-digit [^0-9]<br />

Anchors<br />

Perl-style regular expressions also support additional anchors, as listed in Table 4-10.<br />

Table 4-10. Perl-style anchors<br />

Assertion Meaning<br />

\b Word boundary (between \w and \W or at start or end of string)<br />

\B Non-word boundary (between \w and \w, or \W and \W)<br />

\A Beginning of string<br />

\Z End of string or before \n at end<br />

\z End of string<br />

^ Start of line (or after \n if /m flag is enabled)<br />

$ End of line (or before \n if /m flag is enabled)<br />

Quantifiers and Greed<br />

The POSIX quantifiers, which Perl also supports, are always greedy. That is, when<br />

faced with a quantifier, the engine matches as much as it can while still satisfying the<br />

rest of the pattern. For instance:<br />

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

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

The regular expression matches from the first less-than sign to the last greater-than<br />

sign. In effect, the .* matches everything after the first less-than sign, and the engine<br />

backtracks to make it match less and less until finally there’s a greater-than sign to be<br />

matched.<br />

This greediness can be a problem. Sometimes you need minimal (non-greedy) matching—that<br />

is, quantifiers that match as few times as possible to satisfy the rest of the<br />

Perl-Compatible Regular Expressions | 105<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!