tag:blogger.com,1999:blog-8544609315733972726.post-55183162485261314412007-12-31T17:52:00.004-07:002008-04-17T12:54:08.770-06:00Backreferences in replacements<img width="250" src="http://bp3.blogger.com/_1XYQfEGGEIw/R3nheewrZ5I/AAAAAAAAAD8/cOshhBnDWBM/s400/200712_regex_no_manual_labor.jpg" alt="Say no to manual labor (and use automation in OpenOffice.org)" /> <p><a href="/search/label/openoffice.org%202.4">OpenOffice.org 2.4</a> introduces backreferences in replacements. The feature is also called backward references, subexpression substitutions, or submatches in search and replace. Regardless of the name, the feature opens new, sophisticated possibilities in automation through regular expressions.</p> <p>Backreferences themselves are not new: OpenOffice.org has supported backreferences in the <span style="font-weight: bold;">Search for</span> field, and now you can also use them in the <span style="font-weight: bold;">Replace With</span> field.</p> <h3>Example: Changing YYYY-MM-DD dates to MM/DD/YYYY</h3> <p>Assume you have a long, 100-page Writer document and you need to convert all dates in the format YYYY-MM-DD to MM/DD/YYYYY. It would be tedious to do it manually, so automate it with regular expressions and backreferences.</p> <ol> <li>Open the document in Writer.</li> <li>Choose <span style="font-weight: bold;">Edit &gt; Find &amp; Replace</span> from the menu.</li> <li>In <span style="font-weight: bold;">Search for</span> field, type <code>([0-9]{4})-([0-9]{2})-([0-9]{2})</code></li> <li>In the <span style="font-weight: bold;">Replace with</span> field, type <code>$2/$3/$1</code></li><li>Click the button <span style="font-weight: bold;">More Options</span>.</li> <li>Check the box <span style="font-weight: bold;">Regular Expressions</span>.</li><li>Click <span style="font-weight: bold;">Replace All</span>.</li> </ol> <img src="http://bp1.blogger.com/_1XYQfEGGEIw/R3nWS-wrZ1I/AAAAAAAAADc/LGWt03R5gPY/s400/200712-find_and_replace-YYYYMMDD.png" alt="The find and replace dialog demonstrates how to use a regular expression (also called a regex, regexp) with backreference substitutions (backward references, subexpression substitutions, or submatches)" /> <h3>How it works</h3> <img src="http://lh3.google.com/OOoNinja/R-UgJxZy4lI/AAAAAAAAAvo/VZYIHHSa7ko/s512/200712-diagram-regex.png" border="0" alt="The diagram illustrates how the regular expression with backreference substitution works (OpenOffice.org Writer 2.4+)" /> <p>In the <b>Search For</b> field, each group is enclosed in parenthesis. In the example above, there are three groups. The first matches the year, the second the month, the third the year. In the <span style="font-weight: bold;">Replace With</span> field, each $x code substitutes the matched text found in the respective group. <h3>Example: Redacting a Social Security Number</h3>Say you want to turn many Social Security Numbers (SSN) like 987-65-4320 into a format like XXX-XX-4320. With backreferences in replacement, it is easy to automate. Using the instructions in the first example as a guide, search for this: <code>[0-9]{3}-[0-9]{2}-([0-9]{4})</code> and replace with this: <code>XXX-XX-$1</code></p> <h3>Related articles</h3> <ul> <li><a href="/2007/12/powerful-text-matching-with-regular.html">Introduction to regular expressions</a></li> <li><a href="/2007/12/example-regular-expressions-for-writer.html">Example regular expressions for Writer</a></li> <li><a href="http://www.oooninja.com/search/label/openoffice.org%202.4">OpenOffice.org 2.4 features</a></li> </ul><div class="blogger-post-footer"><a href="http://www.pheedo.com/click.phdo?x=9b46c817936b44038e3def7b73e77e6e&u=%%UNIQUEID%%"><img src="http://www.pheedo.com/img.phdo?x=9b46c817936b44038e3def7b73e77e6e&u=%%UNIQUEID%%" border="0"/></a></div>Andrew Zhttp://www.blogger.com/profile/10108637160465346326noreply@blogger.com