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.
- 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.
- In the Find what field, type: ([\(])(Ref.*)([\)])
Note: There are NO spaces in this string.
- In the Replace with field, type: [\2]
- Click the More button.
- Select the Use wildcards check box.
- Click Find Next then click Replace to test that it works fine. If so, click Replace All.
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.