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.

Changing phone format (TODO) 139AnswerFind (\d{3})[ \-]?(\d{3})[ \-]?(\d{4})Replace :(\1)-\2-\3Let’s focus on the regex we use to match either a hyphen or a whitespace character:[ \-]So there’s a space in there. But what’s with the backslash-hyphen? Recall that hyphens, when insidesquare brackets, become a special character. For example, [a-z], is the character set <strong>of</strong> lowercaseletters from a to z.Thus, when we want to include a literal hyphen inside a square-bracketed character set, we need toescape it with a backslash.One more thing to note: the square brackets act as a sort <strong>of</strong> grouping: thus, the ? makes optionaleither the hyphen or whitespace character (and whatever other characters we want to include inthose square brackets).Exercise: Exactly 10 digits So our web-form will now correctly handle a 10-digit phone numberthat may or may not have hyphens/spaces.But what happens when the user enters in more than 10-digits? If you apply the previous solutionto the following number:25556164040<strong>The</strong> pattern will match this and the replacement will end up as:(255)-561-64040In a real-life application, we do not want our regex to be flexible enough to accept this. Why?Because this kind <strong>of</strong> input indicates that there is an error by the user. Maybe that initial 2 is a typo.Or maybe his finger slipped at the end and added an extra 0. Either way, we do not want our regexto match this. We want the user to know that he possibly screwed up.

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

Saved successfully!

Ooh no, something went wrong!