03.02.2014 Views

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

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.

PCRE Extension ” 147<br />

// Matches ’a’ 1 or more times, however many that may be<br />

$matches = (preg_match(’/a+/’, $string) == 1);<br />

// Matches ’a’ 0 times or 1 time if present, same as ?<br />

$matches = (preg_match(’/a{0,1}/’, $string) == 1);<br />

// Matches ’a’ 0 or more times, same as *<br />

$matches = (preg_match(’/a{0,}/’, $string) == 1);<br />

// Matches ’a’ 1 or more times, same as +<br />

$matches = (preg_match(’/a{1,}/’, $string) == 1);<br />

// Matches ’a’ exactly 2 times<br />

$matches = (preg_match(’/a{2}/’, $string) == 1);<br />

?><br />

N o t e that any use of curly brackets that is not of the form {X}, {X,}, or {X,Y} will be<br />

treated as a literal string <strong>with</strong>in the pattern.<br />

S u b p a t t e r n s<br />

Y o u notice ’ l inl<br />

the examples from the previous section that only a single character<br />

was used. This is because the concept of subpatterns hadn’t been introduced yet.<br />

T ounderstand these, it’s best <strong>to</strong> look an example that doesn’t use them in order <strong>to</strong><br />

understand the effect they have on how the pattern matches.<br />

<br />

Without subpatterns there would be no way <strong>to</strong> match, for example, one or more<br />

instances of the string ’ab’ Su b . p a t t e r n s solve this pattern by allowing individuals<br />

parts of a pattern <strong>to</strong> be grouped using parentheses.<br />

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

Saved successfully!

Ooh no, something went wrong!