Posts Tagged ‘find and replace’

h1

Word: Wildcard find and replace to put parentheses around numbers

June 20, 2019

In another post (https://cybertext.wordpress.com/2018/07/22/word-wildcard-find-and-replace-for-numbers-inside-parentheses/), Thomas asked: “I have to find any number within a document and put it into brackets: 2 -> (2); 12 -> (12); 123 -> (123)”. But he wasn’t having any luck.

A standard Find can use ^# to find any single number (but not multiple numbers), but that command doesn’t work in a wildcard find and replace.

Here’s what I came up with instead; it assumes the numbers are surrounded by a space on both sides—it won’t find any numbers that include punctuation marks (e.g. 1,000,000), that start or finish with a punctuation mark (e.g. comma, period, parenthesis, semicolon, etc.), or those that are in a word (e.g. Model1345):

  1. Press Ctrl+H to open the Find and Replace dialog.
  2. Click More, then select the Use wildcards check box.
  3. In Find What, type: ( )(<[0-9]{1,}>)( ) (NOTE: there is a single space between the first set of parentheses and between the third set)
  4. In Replace With, type: \1(\2)\3
  5. Click Find Next, then click Replace once the first is found. Once you’re happy that it works, repeat until you’ve replaced them all.

What the find and replace ‘codes’ mean:

The three elements (each is enclosed in parentheses) of the Find are:

  1. ( ) — For the first and third elements, you type a set of parentheses, with a space inside the parens.
  2. (<[0-9]{1,}>) — The < and > represent the beginning and end of the ‘word’ respectively (in this case, the ‘word’ is a set of numbers with NO punctuation, e.g. 1, 12, 1234, 7896543); [0-9] represents any number from 0 to 9; {1,} says to look for one or more of the same (i.e. numbers) to any length immediately after that number, thus not limiting the find to only single digit numbers.

For the Replace:

  • \1 — Replaces the first element (the space) with itself
  • ( — Inserts an opening parens
  • \2 — Replaces the second element of the Find with what was in the Find (i.e. a number).
  • ) — Inserts a closing parens
  • \3 — Replace the third element (the space) with itself

 

h1

Word: Find text between quote marks and change to italic

June 5, 2019

All special terms in a Word document I worked on were surrounded by straight double quote marks. I wanted to remove the quote marks and italicise the term. For example, I wanted “term” to become term.

This was a relatively easy task using wildcards in Word’s find and replace, but there are a couple of ‘gotchas’—it won’t catch anything in curly (smart) quotes or inside single quote marks (straight or curly), or if there’s US-style punctuation (e.g. period, comma) at the end, such as “term.” I’ve added alternatives to deal with these situations. It also won’t catch more than one word inside the quote marks, and I don’t have an easy solution for that.

In ALL cases below:

  • you must have Use wildcards checked in the advanced Find and Replace dialog box
  • for the italics, when you’re in the Replace field, select Format > Font> and choose Italic
  • all the double and single straight quotes here DO NOT display correctly, so DO NOT copy/paste from here—instead, type the quote marks in directly from your keyboard

Case 1: Double straight quotes

  • Find: (“)(<*>)(“)
  • Replace: \2

For those preferring to use ACSII codes, a double straight quote mark is ^034.

Case 2: Single straight quotes

  • Find: (‘)(<*>)(‘)
  • Replace: \2

For those preferring to use ACSII codes, a single straight quote mark is ^039.

Note: This may not work—if it doesn’t, try (‘)(*)(‘) as the Find, but be careful when replacing as a single quote mark is also used as an apostrophe. DO NOT do Replace All.

Case 3: Double curly quotes

  • Find: (“)(<*>)(”)
  • Replace: \2

For those preferring to use ACSII codes, an opening double curly quote mark is ^0147 and a closing one is ^0148.

NOTE: It’s easier to copy a curly quote from the main Word document and paste it into the Find. Don’t forget to copy an opening one for the left part of the Find string, and a closing quote for the right part.

Case 4: Single curly quotes

  • Find: (‘)(<*>)(’)
  • Replace: \2

For those preferring to use ACSII codes, an opening single straight quote mark is ^0145, and a closing single straight quote mark is ^0146.

Note: This may not work—if it doesn’t, try (‘)(*)(‘) as the Find, but be careful when replacing as a single quote mark is also used as an apostrophe. DO NOT do Replace All.

NOTE: It’s easier to copy a curly quote from the main Word document and paste it into the Find. Don’t forget to copy an opening one for the left part of the Find string, and a closing quote for the right part.

Case 5: Period or comma inside the closing quote mark

Use the relevant Find from any of the above, depending on the style of quote marks you’re looking for, and add an extra command ([,.]) to find the comma or period too. For straight double quotes, you’d change it to:

  • Find: (“)(<*>)([,.])(“)
  • Replace: \2\3

If you want to keep the punctuation, then you need to add \3 to the Replace. If you don’t want to keep the punctuation, then just leave it as \2. Again, don’t forget to set the Replace to italic font. The end result will be an italicised word with its trailling punctuation also in italics.

If don’t want the punctuation in italics, then you’ll need to run another find/replace using wildcards to change the punctuation back to normal text:

  • Find: ([,.]) (this time, set the Find to italics using Format > Font > Italic)
  • Replace: \1 (for this one, set the Replace to NOT use italics using Format > Font > Regular)

 

 

h1

Word: Wildcard find and replace for a space after a special character

June 4, 2019

Wildcard find and replace in Word is brilliant, but sometimes it just doesn’t work.

I had set up a find and replace routine to find any of >, <, ≥, and ≤ followed by a space then a number (e.g. > 25). I wanted to remove the space from each I found (e.g. >25). The syntax I used for the find was ([><≥≤])( )([0-9]) (Note: there’s a space inside the second set of parentheses). And the replace I had was \1\3. Looks fine, right? But it didn’t work! Instead, I got some strange results with various numbers preceded by a space replaced with ‘\1\3’. The ≥≤ part worked fine, but not the >< part.

And then I remembered that certain characters have special meaning in Word’s wildcard find and replace—two of which were < for the beginning of a word, and > for the end of a word. No wonder I was getting weird results.

As with any other special characters (e.g. ?, *, [, ], etc.) you have ‘escape’ them for Word to treat them as a normal character, not a special character. The escape character is \ and when I added that in front of each of the special characters, the wildcard find and replace worked as it should.

Here’s what did work:

  • Find: ([\>\<≥≤])( )([0-9])
  • Replace: \1\3 

 

h1

Word: Find multiple manually entered numbers and delete

April 22, 2019

I copied a very long manually numbered list (more than 300 numbered list items) from the internet into Notepad (to strip out the formatting), then into a Word document. Unfortunately, the numbers remained, and applying Word’s numbering didn’t get rid of them. What to do? Use Word’s Find and Replace with wildcards, of course!

My aim was to delete all the numbers and the space, en dash, space following each number, to end up with a list I could apply Word’s auto numbering to.

Here’s how I did it (NOTE: If you’re doing something similar, work on a COPY of your document first to make sure this works as you want it to):

  1. Open the Find and Replace dialog box (Ctrl+h).
  2. Click More.
  3. Select the Use Wildcards checkbox.
  4. In the Find what field, type: (<[0-9]@>)( – )
  5. Leave the Replace with field empty.
  6. Click Replace All.

Voilà! Almost all the numbers were gone, except for a few that used hyphens instead of en dashes or that didn’t have a space before or after the en dash, but I spotted these easily and fixed them manually.

How this works:

  • The first element (in the first set of parentheses) comprises several parts:
    • < and > indicate the beginning and end of a ‘word’, respectively
    • [0-9] indicates any number in the range from 0 to 9, and with the < in front of it, any ‘word’ that starts with a numeral
    • @ says to look for whatever immediately preceded this symbol as many times as required (i.e. a number from 0 to 9) until you reach the end of word marker (in other words, a whole number of any length)
  • The second element (second set of parentheses) looks for a space, en dash, space immediately following the number found in the first element
  • By leaving Replace with empty, you’re replacing whatever was found that matched the Find with nothing—in other words, you’re deleting whatever was found.

 

h1

Word: Find and replace a custom font colour

March 30, 2019

If you’ve used a custom colour for quite a bit of text in your document and want to replace it with another custom colour, you have two options:

  • If the colour is part of a style, modify the font colour in the style’s settings—this should change all instances immediately. I won’t discuss this further.
  • If the colour has been applied manually (i.e. not part of a style), then you’ll need to do a find and replace to find all instances of colour A and replace them with colour B.

Here’s how to change a manually applied custom colour using find and replace:

  1. Open the Find and Replace dialog (Ctrl+H).
  2. Make sure the cursor is in the Find field.
  3. Click More.
  4. Click Format.
  5. Choose Font.
  6. Click the drop-down arrow for Font Color.
  7. Click More Colors.
  8. Enter the RGB values you want to find (e.g. 255, 51, 153).
  9. Click OK. The text under the Find field should show the RGB values you selected; for example: Font Color: Custom Color (RGB(255,51,153)).
  10. Place your cursor in the Replace field.
  11. Repeat Steps 4 to 7.
  12. Enter the RGB values you want to use for the replace (e.g. 230, 131, 76).
  13. Click OK. The text under the Find field should show the RGB values you selected; for example: Font Color: Custom Color (RGB(230,131,76)).
  14. Click Find Next, then click Replace for the first one found.
  15. Repeat Step 14 until all are found, or, if you are confident that you won’t mess up anything else, click Replace All.

 

h1

Word: Find and replace multiple asterisks used as separator lines

February 15, 2019

On one of my editing forums, someone had a situation where the author had used many long strings of asterisks to separate various parts of the document. The length of these asterisk ‘lines’ weren’t the same—sometimes the author had used 3 asterisks, other times they’d used 4 or 8 or 35 or some other random number of asterisks. How to get rid of them all at once? Enter Word’s wildcard find and replace feature!

  1. Press Ctrl+H to open the Find and Replace dialog box.
  2. Click the More button to show more find/replace options.
  3. Select the Use wildcards checkbox.
  4. In the Find what field, type: (^013)(\*{2,50})(^013)
  5. In the Replace with field, type: \1\3
  6. Click Find Next to find the first instance, then Replace to replace the multiple asterisks with a paragraph mark.
  7. Repeat step 6 as many times as you need to be confident that it’s finding the right things. Once you’re confident, click Replace All to run through the whole document and fix all instances.

Explanation for how this works:

  • (^013) looks for a paragraph mark (this indicates the end of the previous line of text). This string defines the first section of the Find.
  • (\*{2,50}) looks for two or more asterisks, up to 50 asterisks (yes, you can change the numbers inside the curly braces to suit your situation— if you think you might have some lines with 100 asterisks, then change the 50 to 100; if you don’t know what the upper limit is, then leave it empty [i.e. use \*{2,}). This string defines the second section of the Find. NOTE: An Asterisk is a special character in wildcard searches, so you need to ‘escape’ it by putting a backward slash in front of it.
  • (^013) looks for a paragraph mark immediately following a string of asterisks. This string defines the third and last section of the Find.
  • \1 replaces the first part of the wildcard string with itself. In other words, the paragraph mark found is replaced with itself, so no change apparently occurs.
  • \3 replaces the third part of the wildcard string with itself. In other words, the paragraph mark found is replaced with itself, so no change apparently occurs.

A note about ^013: In an ordinary find/replace, you would use ^p for a paragraph mark, but this doesn’t work in a wildcard find/replace—instead, you have to use ^013.

h1

Word: Insert a degree symbol

September 22, 2018

There’s a special character for a degree, so don’t make the mistake of superscripting a lower case ‘o’. Instead, use one of these methods to insert a proper degree symbol in Microsoft Word:

  • If you have a separate number pad, then press Alt+0176 (press and hold the Alt key while you type 0176)
  • For any keyboard with or without a number pad, press Ctrl+Shift+@.
  • Go to the Insert tab > Symbol — the degree symbol is character code = 00B0, Unicode (hex)
  • If you have Math AutoCorrect turned on, then type \degree (to turn on Math AutoCorrect: File > Options > Proofing > AutoCorrect Options > Math AutoCorrect tab).

If you have a lot of superscripted lower case ‘o’ characters used instead of a proper degree symbols, you can search for them and replace them with the correct symbol:

  1. Open the Find and Replace window (Ctrl+h).
  2. In the ‘Find what’ field, type a lower case o.
  3. With your cursor still in the ‘Find what’ field, click More.
  4. Click Format and select Font.
  5. Click the Superscript checkbox until it has a check mark in it.
  6. Click OK to close the Find Font window.
  7. Put your cursor in the ‘Replace with’ field.
  8. Type ^0176
  9. With your cursor still in the ‘Replace with’ field, click Format and select Font.
  10. Click the Superscript checkbox until it is clear. You may have to click it twice.
  11. Check your Find and Replace window looks like the screenshot below. If it does, click Find Next and then Replace for each one found.

Related: Prime and double prime symbols: https://cybertext.wordpress.com/2018/09/23/word-insert-a-prime-and-double-prime-characters/