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.

Capture, Reuse 8805-14-8912-03-9803-15-9902-01-0007-07-0103-08-0201-22-12<strong>The</strong> reasoning was that it’s “obvious” that 98 refers to 1998 and 02 refers to 2002. Of course, it won’tbe so obvious as we get farther into the 21st century. So write a regex that coverts the two-digityears into four-digits:05-14-198912-03-199803-15-199902-01-200007-07-200103-08-200201-22-2012AnswerThis is a little bit tricky, not least <strong>of</strong> which because using a regex here is not recommended in reallife. If you’re working with a big dataset, you might have dates that include 1902 along with 2002.And so you don’t want to use a dumb regex to deal with that ambiguity, you’ll likely need to writea programming script with some logic and error-handling.However, sometimes you just need a quick and dirty solution if you know your data doesn’t containthose ambiguities. In this case, we’ll need to use two regex operations, one each for pre-2000 andpost-2000 dates.For pre-2000:Find ([89]\d)$Replace 19$1For post-2000:Find ([01]\d)$Replace 20$1Exercise

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

Saved successfully!

Ooh no, something went wrong!