Posts Tagged ‘wildcards’

h1

Word: More find and replace with wildcards

April 20, 2012

I needed to make a global change to some text that the author had written incorrectly (i.e. not according to our house style guide).

Scenario

They had written [Ref 1] for a reference instead of our style of [Ref. 1], which not only has a full stop (period) after the f but also uses a non-breaking space to separate the full stop and the following numeral. All the numbers were different. Some were a single number, while those from References 10 and higher were two-digit numbers. There were a LOT of references like this in the document.

So I used my knowledge of find and replace wildcards to globally make the change in just a few seconds.

Solution

This solution works in Word 2003, Word 2007, and Word 2010.

  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)( )([0-9])
    Note: There is only ONE space in this string — it’s between the two parentheses that don’t appear to enclose anything. The 0-9 bit is a zero, not an ‘o’ for orange.
  3. In the Replace with field, type: \1.^s\3
    Note: There are NO spaces in this string and the s must be in lower case. Don’t forget the full stop!
  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.

Explanation for how this works:

  • (Ref) looks for the string of letters: Ref
  • ( ) looks for a space immediately after Ref (i.e. no punctuation)
  • ([0-9]) looks for any number that follows immediately after Ref and its following space. The square brackets indicate a range — in this case any number from 0 to 9 will be found. It doesn’t matter whether the numbers are one or two-digit numbers — the critical thing the find/replace is looking for is any numeral after Ref<space>.
  • \1 replaces the first part of the Find string with itself (in other words, Ref gets replaced with Ref)
  • .^s replaces the second part of the Find string (the space) with a full stop followed immediately by a non-breaking space. For a non-breaking space, you MUST use a lower case s and precede it with the ^ (Shift+6). Note: If you just want an ordinary space — not a non-breaking one — then use \2 instead of .^s.
  • \3 replaces the third part of the Find string with itself (in other words, the number found gets replaced with the same number).
h1

Word: Replace and reformat text inside square brackets using wildcards

June 20, 2011

My husband wanted to select a long column of text and find any text that was inside square brackets and reformat it so that the text — and the square brackets — was 4 pt and blue (no, I don’t know why either…).

This is an ideal job for using wildcards in Word’s find and replace. However, square brackets are special characters in wildcard searches, so they have to be treated differently. With some help from http://word.mvps.org/faqs/general/usingwildcards.htm and a bit of trial and error, I figured it out. I explain what all the settings mean after these steps, if you’re interested. Meantime, here’s my solution, which works in all versions of Word:

  1. Select the text you want to change (e.g. entire document, selected paragraphs, selected columns or rows of a table).
  2. Press Ctrl+H to open the Find and Replace dialog box.
  3. Click the More button.
  4. Select the Use wildcards check box.
  5. Put your cursor into the Find what field.
  6. Type the following exactly (or copy it from here): (\[)(*)(\])
  7. Go to the Replace with field and type: \1\2\3
  8. Click the Format button, and select Font.
  9. On the Font dialog box change the settings to what you want — in my husband’s case, this was 4 pt and blue — then click OK. Your Find and Replace dialog box should now look like this:

    Find and reformat text inside square brackets

    Find and reformat text inside square brackets

  10. Click Replace All.
  11. Once all replacements have been made, check that you got what you expected before making further changes to the document. If it’s all OK, save your document with the new changes.

What it all means

The three elements of the Find are:

  1. (\[) -- You need to find a specific character (the opening square bracket), so you need to enclose it in parentheses. However, because the square brackets are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash '\' (also known as an 'escape' character) before the [.
  2. (*) -- This tells Word to look for any characters after the opening square bracket. There's no limit on what sort of characters (alpha, numeric, or symbols) Word is to find, or on how many there are.
  3. (\]) — This tells Word to stop the find at the first closing square bracket it finds after an opening square bracket followed by any other characters. As with the opening square bracket (1. above), the closing square bracket is a special wildcard character, so needs a backslash in front of it for Word to treat it as ordinary text, and it needs to be enclosed in parentheses as it’s an exact match you want.

There are no spaces between any of these elements — the aim is to find a string such as [green frog] and replace it with exactly the same text but formatted in a different color and with a difference font size.

The three elements of the Replace are:

  1. \1 — Tells Word to replace the first element of the Find with what was in the Find (the opening square bracket).
  2. \2 — Tells Word to replace the second element of the Find with the same text as what was found. In other words, keep the exact text as was found, but change it’s font size and color.
  3. \3 — Tells Word to replace the third element of the Find with what was in the Find (the closing square bracket).

As with the Find elements, there are no spaces between these elements. You still want [green frog], not [ green frog ].

See also:

[Links last checked June 2011]

Follow

Get every new post delivered to your Inbox.

Join 232 other followers