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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data Cleaning with the Stars 130LORD OF THE RINGS: THE RETURN OF THE KING, THELet’s break this into two steps; first, the article: Use the beginning-<strong>of</strong>-line anchor and the alternationoperator to capture just the “THE”’s at the start <strong>of</strong> a movie’s title:^(THE|AN?)<strong>The</strong>n the rest <strong>of</strong> the title can be captured with the dot operator:(.+)All together:Find ˆ(THE|AN?) (.+)Replace \2, \1This results in a list <strong>of</strong> titles that can be sorted alphabetically:AAMERICAN IN PARIS, ANAPARTMENT, THEBEAUTIFUL MIND, ABIG LEBOWSKI, THEFISTFUL OF DOLLARS, AGODFATHER, THEGOOD, THE BAD, AND THE UGLY, THEGREAT GATSBY, THEKING AND I, THELORD OF THE RINGS: THE RETURN OF THE KING, THESTAR TREK II: THE WRATH OF KHANAs a bonus, notice that our regex doesn’t screw up the movie titled A by converting it to , A. Ourregex requires that at least one space and a non-newline character exist on the same line.Make your own delimitersSometimes interesting data comes to you in clumps. Here’s a sample <strong>of</strong> movies with IMDb userratings²⁶:²⁶http://www.imdb.com/chart/top

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

Saved successfully!

Ooh no, something went wrong!