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 83Roosevelt, Franklin D.Walker, Mary E.Berners-Lee, Tim J.Williams, Serena J.<strong>The</strong> main variation here is that we need a third capturing group to contain the middle initial. Also,the names may contain hyphens.Find ([\w-]+), ([\w-]+) ([A-Z].)Replace $2 $3 $1A couple <strong>of</strong> notes:1. <strong>The</strong> hyphen inside square brackets has to be escaped, because, the hyphen in that context is aspecial character that acts as a range (e.g. from A to Z, as we see in the third-capturing group).2. <strong>The</strong> dot character – . – has to be escaped, because it is a special regex character. In fact, it isa very powerful character that we’ll learn about in a [later chapter]{#dot-character}.Correcting dates with capturing groupsIn a previous chapter, we encountered the problem <strong>of</strong> trying to fix the year-value on a list <strong>of</strong> dates:12/24/2012, $50.0012/25/2012, $50.0012/28/2012, $102.001/1/2012, $2012.001/2/2012, $32.001/6/2012, $52012.001/12/2012, $642.001/13/2012, $500.001/20/2013, $100.002/1/2013, $2012.20If this list is chronological, then the data-entry person forgot to go from 2012 to 2013 when Januaryrolled around.Let’s start with how we selectively target the January dates:Find 1/\d+/2012

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

Saved successfully!

Ooh no, something went wrong!