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.

Capture, Reuse 90Mary asked: "What in the world?".James yelled: "Hello!".Mary responded: "Goodbye!".Using parentheses without capturingWhat if we want to use parentheses just as a way to maintain order <strong>of</strong> operations?To make a set <strong>of</strong> parentheses non-capturing, simply add a question-mark and colon immediatelyafter the left-brace:(?:this-pattern-isnt-captured)(this is captured)Note: To re-emphasize, these symbols must show up one after the other in that exact order.Ergo: (?:x) is not at all the same as: ( ?:x)Given this text:1. J.R. Ewing2. A.C. Thompson3. T.A.L. GlassAnd wanting to change it to:1. Ewing2. Thompson3. Smith<strong>The</strong> pattern we seek to match is:• A number followed by a period (captured)• A series <strong>of</strong> uppercase letters, each followed by a period (but not captured)• A string <strong>of</strong> word-characters (captured)This is the Find-and-Replace:Find (\d+.) ([A-Z].)+ (\w+)

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

Saved successfully!

Ooh no, something went wrong!