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.

Negative character setsNow that we know how to include sets <strong>of</strong> characters in our patterns, let’s see how to exclude them.<strong>The</strong> syntax is simple and easy to remember. But it opens up a new range <strong>of</strong> regex possibilities. Sothis chapter will re-emphasize the concepts we’ve covered so far while mixing in new tricks thatcome from thinking negatively.With the square-bracket notation, we are able to specify a set or range <strong>of</strong> characters to match:Find [bcd]ogMatches “bog” or “cog” or “dog”But what if we want to match anything but those terms, such as “fog” or “hog”?Negative character setsTo exclude a set <strong>of</strong> characters from a pattern, use the same square bracket notation as a normalcharacter set, but use a caret symbol, ˆ, directly after the left-bracket.<strong>The</strong> following regex:[^abcd]– will match any character except a literal a, b, c, or dThis regex:[^0-5]– matches any character that is not the numbers 0 through 5.Let’s revisit a previous exercise in which we removed all the non-number/decimal characters:75

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

Saved successfully!

Ooh no, something went wrong!