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.

Changing phone format (TODO) 142`1-555-212-0000`But not:`-555-212-0000`A little obsessive-compulsive, maybe, but let’s just do it for practice.What’s the pattern?We’re looking for the standard 10-digit number that may be preceded by a 1 and either a hyphen ora whitespace character (compare this to the previous pattern we looked for).Remember from the previous chapter that parentheses act as a way to group parts <strong>of</strong> a patterntogether. So let’s try the following:Find ˆ(1[ \-])?(\d{3})[ \-]?(\d{3})[ \-]?(\d{4})$Replace (\1)-\2-\3Apply it to the following sample input:55524150101 555 981 27051-9112400000-101 555 7745And you will get:()-555-241(1 )-555-981(1-)-911-240-101 555 7745What the? <strong>The</strong> very last number wasn’t affected, which is what we wanted. But the matchedpatterns got replaced by non-sensical values.<strong>The</strong> problem is our use <strong>of</strong> an extra set <strong>of</strong> parentheses without modifying our Replace value.Remember that when using backreferences, the numbers correspond to each sequential capturedgroup.In other words, \1 no longer refers to (\d{3}) but to (1[ \-]), which is simply the optional 1-prefix.<strong>The</strong>re are two ways to fix this. <strong>The</strong> (intellectually) lazy (but more physically demanding) way is toalter the Replace field to correspond to the extra captured group:

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

Saved successfully!

Ooh no, something went wrong!