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.

LookaroundsThis chapter covers a regex technique that allows us to test if a pattern exists without actuallyreplacing it.Consider the following text:100 BROADWAY ST. NEW YORK NY 10006 UNITED STATESTurn it to:100 BROADWAY ST., NEW YORK, NY, 10006, UNITED STATESTKDOTODNew York NY 10006 United StatesBrooklyn NY 11035 United StatesQueens NY 12006 United StatesAnd consider the desired transformation: putting a comma after the city name:New York, NY 10006 United StatesBrooklyn, NY 11035 United StatesQueens, NY 12006 United States<strong>The</strong> regex is simple enough with two capturing groups:Find :([A-Za-z ]+) ([A-Z]{2} \d{5})Replace :\1, \2All we do with the second part <strong>of</strong> the pattern – e.g. NY 10006 – is echo it back, after the firstbackreference has a comma appended to it. Yet it seems our regex needs to confirm that the secondpart exists.Lookarounds allow us to test for that existence without using another capturing group just to echothese “tested” characters.It’s possible to get by without knowing lookarounds using everything we’ve learned so far. So don’tsweat it if it seems too complicated. Lookarounds are just a useful technique and shed some lighton the inner workings <strong>of</strong> the regex engine, something this book does not spend much time thinkingabout.Positive lookaheadA positive lookahead is denoted with a question-mark-and-equals sign inside <strong>of</strong> parentheses:105

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

Saved successfully!

Ooh no, something went wrong!