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.

Optionality and alternation 97they'retheyretheirthereAnswerFind the[iry]'?[er]Note: while this is an amusing exercise, this regex is so unreadable that you’re better <strong>of</strong>f just usingstraight-up alternation:their|there|they'?reExercise: Zip codesIn the United States, zip codes can come in at least two different forms: five-digit and nine-digit:10020-90201001860201-3277In the following city listings, add commas to delimit the state, zip code, and country information.Change:New York 10020-9020 United StatesCalifornia 90210 U.S.To:New York, 10020-9020, United StatesCalifornia, 90210, U.S.Answer<strong>The</strong> optional part <strong>of</strong> the pattern is the hyphen and the four numerical digits:Find ( \d{5})(-\d{4})?Replace ,\1\2,

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

Saved successfully!

Ooh no, something went wrong!