13.07.2015 Views

Download - The Bastards Book of Regular Expressions

Download - The Bastards Book of Regular Expressions

Download - The Bastards Book of Regular Expressions

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.

Laziness and greediness 100Being too greedyIn the previous chapter, we learned about the dot operator, which will match any kind <strong>of</strong> character.This seems like just what we need, right?Given the following text:<strong>The</strong> sheep says “Cowabunga!” to those who approach it.Find ".+"Replace "Hello"<strong>The</strong> result is:<strong>The</strong> sheep says “Hello” to those who approach it.However, there’s a serious problem with this solution. Given a more complicated phrase:When the sheep says “Cowabunga!”, the cow will reply, “Baa! Baa!”– when we apply the previous solution, we end up with:When the sheep says “Hello”What happened? We got burned by greediness.Let’s express the regex – ".+" – in English: Match a quotation mark and then match every characteruntil you reach another quotation mark.That seems straightforward. But consider the power <strong>of</strong> the dot: it matches any character, except fornewline characters. And “any character” includes the quotation mark.So in our example text:When the sheep says “Cowabunga!”, the cow will reply, “Baa! Baa!”<strong>The</strong>re’s at least three ways that the regex engine can satisfy our pattern:It could stop at the second quotation mark:When the sheep says “Cowabunga!”, the cow will reply, “Baa! Baa!”

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

Saved successfully!

Ooh no, something went wrong!