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 81Parentheses for captured groupsParentheses have what might be called a major side effect regarding the patterns they group together.When part <strong>of</strong> a regex is inside a parentheses, the regex engine contains a backreference to thepattern captured inside the parentheses.Let’s go back to the last-name-first-name example at the beginning <strong>of</strong> the chapter:Smith, ShellyPerry, DwayneWhat’s the pattern?We want to capture the words on both sides <strong>of</strong> the comma.This is easy enough; the regex pattern is:(\w+), (\w+)Ah, but what do we replace it with?Backreference notationUp until now, the Replace part <strong>of</strong> the Find-and-Replace action hasn’t involved many specialcharacters. In fact, nearly none <strong>of</strong> the special characters in regex-land, such as +, [ ], ˆ, and s<strong>of</strong>orth, have any meaning beyond their literal values.But the backslash still wields its escaping power in the Replace field.Backreference notationInside the Replace field, using a backslash followed by a number, will denote thevalue inside the corresponding captured group. In other words, \1 contains thevalue <strong>of</strong> the first captured group. And \2 contains the value for the second group,and so forth.Let’s consider this simple use <strong>of</strong> parentheses in a pattern:(ab)(cd)(e)If the Replace field is set to:.\1\2\3

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

Saved successfully!

Ooh no, something went wrong!