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.

Dating, Associated Press Style (TODO) 147Abbreviate the monthAs you can see from the above examples, all months more than 4 characters long are abbreviated to3 letters* with a dot. <strong>The</strong> warm months <strong>of</strong> May, June, and July are left unabbreviated.* Note: September is abbreviated to Sept. in AP style.<strong>The</strong> literal solution<strong>The</strong>re’s only – and will always only be – 12 months, nine <strong>of</strong> which are actually affected. So withoutmuch thinking, we can match them literally with the use <strong>of</strong> alternationFind (January|February|March|April|August|September|October|November|December)Replace ???Unfortunately, it’s not that easy. This pattern makes a match, but what do we replace it with?Capturing the entire month’s name gives us a backreference to…the entire month’s name, which isunhelpful if we’re trying to abbreviate the month.We need to capture the first three characters <strong>of</strong> the long months and discard the rest. Let’s also usea bracekted character set to match (but not capture) the rest <strong>of</strong> the month’s name to discard:Find (Jan|Feb|Mar|Apr|Aug|Sept|Oct|Nov|Dec)\w+ Replace\1.Try it on this list <strong>of</strong> dates:February 1, 2012January 19, 1999May 6, 2014March 22, 1788June 7, 1960Match just full dates<strong>The</strong>re’s one twist to AP Style dates: only dates that include a specific day use the abbreviated monthstyle.So our pattern needs to become more specific. It must not affect non-exact dates, such as:• <strong>The</strong> president last visited the state in October 1980

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

Saved successfully!

Ooh no, something went wrong!