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.

Anchors: A way to trim emptiness 61Using the $, however, requires no extra replacement action. It simply asserts that the pattern containsthe end-<strong>of</strong>-the-line, which isn’t an actual character, per se, in the text.In other words, the $ only detects the end-<strong>of</strong>-the-line, and so no replacement is actually done at thatposition.Try it out yourself. In your Find field, match the $ operator and then replace it with nothing:nothing will happen to the text (compare this to replacing \n with nothing).So let’s strip the trailing commas using the $ operator:Find ,$Replace (with nothing)In English We want to find the comma right before the end-<strong>of</strong>-the-lineEscaping special charactersIf the caret and the dollar sign denote the beginning and the end <strong>of</strong> the line, respectively, then itfollows that the following patterns don’t make much sense:abc^ByeHello$worldHow would abc come before the beginning <strong>of</strong> the line yet be on the same line as Bye? Or how couldworld be on the same line as Hello and the end <strong>of</strong> the line character, $?So, with what we know so far, it makes sense that the caret and dollar sign typically serve as bookendsfor a regex pattern. However, the following regex may be confusing:He gave me \$10+This regex matches the text in bold below:He gave me $100 yesterday<strong>The</strong> key character here is our friend the backslash. We’ve seen how the backslash, when precedingthe letters b and n, give them special meaning: a word boundary and newline character, respectively.What happens when the backslash precedes an already special character, such as the dollar sign?<strong>The</strong>n it’s just a literal dollar sign, as we see in the above example.Exercise: Remove leading dollar signsis mistakenly repeated:Given this list <strong>of</strong> dollar amounts in which the dollar sign

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

Saved successfully!

Ooh no, something went wrong!