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.

Matching any letter, any numberOf course, we’ll need to work with actual alphabetical letters, numbers, and symbols other thanwhitespace and newlines. <strong>Regular</strong> expressions have a set <strong>of</strong> shortcuts and syntax for this purpose.<strong>The</strong> numeric character classUsing our friend the backslash, we can turn a literal letter into a special character to match numbers.When the letter d is preceded by a backslash, the pattern no longer matches the literal letter d, butany numerical digit from 0 to 9:\d+Matches:<strong>The</strong>re are 10010 sheepWe refer to \d as a “character class” or “character set” because it affects a set <strong>of</strong> characters. In thiscase, \d matches the set (i.e. “class”) <strong>of</strong> characters that are numbers.Let’s try out the \d in a simple exercise.Exercise: Redact the numbersGiven this phrase:<strong>The</strong> robbery was reported on January 12, 2013. At 9:40, the suspect is alleged to haveentered the pizzeria at 120 Broadway and stolen 9 bags <strong>of</strong> oregano.Replace all the numbers with the letter X63

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

Saved successfully!

Ooh no, something went wrong!