h1

Word: Changing parentheses to square brackets while preserving the contents

September 15, 2014

I needed to make a global change to convert numbered citations in parentheses that looked like this (Ref. 1) to citations in square brackets: [Ref. 1].

Sounds simple, right? Just do a find for (Ref. and replace it with [Ref. Well, that would work for the opening parentheses, but what to do with closing ones? I couldn’t just do a global replace for all closing parentheses as that would replace every one in the document, whether they were part of a citation or not.

To complicate matters, all the citation numbers were cross-referenced fields, which meant I couldn’t just search for the numbers 0 to 9 followed by a closing parenthesis and replace those otherwise I’d replace a field code with plain text, thus breaking the cross-reference.

And what about citations that had more than one numeral (e.g. Ref. 25 or Ref. 123)? Or more than one citation separated by a semicolon (e.g. Ref. 25; Ref. 123)?

I needed to find all opening and closing parentheses surrounding one or more Ref. instances, change the parentheses to square brackets and make sure all the contents (text, spaces, non-breaking spaces, and fields) in between were preserved.

This was a job for find/replace using wildcards!

I had to do a bit of experimenting to get the Find expression correct, but once I did I was able to replace all in a few seconds.

Solution

  1. In the Word document where you want to make this change, press Ctrl+H to open the Find and Replace dialog box; the Replace tab should be in focus.
  2. In the Find what field, type: ([\(])(Ref.*)([\)])
    Note: There are NO spaces in this string.
  3. In the Replace with field, type: [\2]
  4. Click the More button.
  5. Select the Use wildcards check box.
  6. Click Find Next then click Replace to test that it works fine. If so, click Replace All.

 

Find_ref

Explanation for how this works:

  • ([\(]) looks for an opening parenthesis. As an opening parenthesis is a special character, you have to ‘escape’ it with a backslash, AND surround that expression with square brackets and then surround the whole thing with parentheses to create a single unit. This is the first part of the expression.
  • (Ref.*) looks for the string of letters Ref followed by a period and any number of characters (represented by the *). This takes care of citations with two or more numbers, and one or more citations within the parentheses. This is the second part.
  • ([\)]) looks for a closing parenthesis. As with the first one, this parenthesis is a special character, you have to ‘escape’ it with a backslash, AND surround that expression with square brackets and then surround the whole thing with parentheses to create a single unit (the third part).
  • [\2] replaces the first part with an opening square bracket, then it replaces the second part with itself (thus preserving the fields and all the content with the parentheses), then replaces the third part with a closing square bracket.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: