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.

Anchors: A way to trim emptinessIn a [previous chapter]{#plus-operator}, we learned how to match consecutive newline characters.This is handy when we want to remove blank lines from a text file.Another annoying whitespace problem occurs when there is padding between the beginning <strong>of</strong> theline and the first actual text character. For example, given these haikus¹⁶:do what you feel like since the work is abandoned the law doesn’t careif you use this code you and your children’s children must make your source freeI want them to be formatted so that the leading whitespace is removed and the lines are flush againstthe left-margin:do what you feel like since the work is abandoned the law doesn’t careif you use this code you and your children’s children must make your source free<strong>The</strong>se “license haikus” are courtesy <strong>of</strong> Aaron Swartz¹⁷<strong>The</strong> caret as starting anchorWe need a way to match the beginning <strong>of</strong> the line. Think <strong>of</strong> it as a word-boundary &dash; remember\b? – except that we want a boundary for the entire line.To have a regex include the beginning <strong>of</strong> the line, use the caret: ˆ<strong>The</strong> following regex:^hello– will match the word hello if hellooccurs at the beginning <strong>of</strong> the line, as in the following example:hello goodbyeHowever, ˆhelloˆ will not match the hello in the following line:goodbye hello¹⁶http://www.aaronsw.com/weblog/000360¹⁷http://www.aaronsw.com/weblog/00036056

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

Saved successfully!

Ooh no, something went wrong!