Posts Tagged ‘find and replace’

h1

Word: Find text with angled brackets and replace the text with a character style

November 2, 2019

Here’s one I heard of today, and how I solved it. The person has a Word document with various words and phrases surrounded by double angled brackets, as in <<this is my phrase>>. They want to apply a character style to any text surrounded by angle brackets, and delete those brackets.

I always like a challenge like this, so here’s my solution:

  1. Press Ctrl+H to open the Find and Replace window.
  2. Click More.
  3. Select the Use wildcards checkbox.
  4. In the Find what field, type: (\<\<)(*)(\>\>)
  5. In the Replace with field, type: \2
  6. Click Format, then select Style.
  7. Choose the character style you want to apply (character styles are indicated with a lowercase ‘a’ to their left), then click OK.
  8. The character style’s name should be listed directly underneath the Find what box—make sure that this is the case.
  9. Click Find Next, then click Replace if you are satisfied that the correct phrasal structure has been selected.
  10. Assuming that replace worked as you wanted it to, and you are confident the result looks as you expect, click Replace All. (If in doubt, keep clicking Find Next, then Replace.)

How this works:

  • The Find is broken up into three elements, each surrounded by parentheses:
    • The first element contains the opening two angled brackets.
    • The second contains an asterisk wildcard character for any character and any number of characters.
    • The third contains the closing two angled brackets.
  • In the first and third elements of the Find, the angled brackets are each preceded by a \ because an angle bracket is a special character in wildcard searches and therefore must be ‘escaped’ so that Word treats it as a normal character. The ‘escape’ character is the \
  • In the Replace, \2 tells Word to replace the second element (the text and all characters between the angled brackets) with itself (i.e. make no changes), and to apply the character style you chose to that replaced text.
  • By omitting \1 and \3 in the Replace (representing the first and third elements of the Find), Word will effectively delete the opening and closing angled brackets.
h1

Word: Find and highlight words of two or more capitals

October 31, 2019

I previously wrote about using wildcards in Word to find abbreviations, acronyms, and initialisms that used two or more capital letters, but that post didn’t address how to highlight these so that you can identify them easily when compiling a list of abbreviations. This one does.

  1. Make sure you have a highlight colour selected (Home tab, Font group) that isn’t used for anything else.
  2. Press Ctrl+H to open the Find and Replace window.
  3. Click More to show further options.
  4. Select the Use wildcards checkbox.
  5. In the Find what field, type: (<[A-Z]{2,}>)
  6. In the Replace with field, type:  \1
  7. With your cursor still in the Replace with field, click Format (bottom of the window) then Highlight. The word ‘Highlight’ should display immediately below the Replace with field.
  8. Click Find next to find the first string of two or more capitals.
  9. If this is an acronym, abbreviation, or initialism, click Replace. The term remains the same but should now be highlighted in the colour you chose in Step 1.
  10. Repeats Steps 8 and 9 to jump to and/or highlight the next string of two or more caps.
  11. Optional but not recommended: If you are confident that the only strings of capital letters in your document are acronyms etc., then click Replace All. Note: Every string of capitals will be highlighted, even those that are repeats of ones you highlighted earlier and those that aren’t acronyms etc. (e.g. document numbers, fully capped words).

How this works:

  • The opening and closing parentheses contain the Find command and allow you to reference it in the Replace.
  • The opening and closing arrow brackets (< and >) specify that you want a single whole word, not parts of a word. Without these, you would find each set of caps (e.g. in the string ABCDEF, you would find ABCDEF, then BCDEF, then CDEF, then DEF, then EF, before moving on to the next set of caps).
  • [A-Z] specifies that you want a range (the [ ] part) of caps that fall somewhere in the alphabet (A-Z). If you only wanted capped words that started with, say, H through to M, then you’d change the range to [H-M] and all other capped words starting with other letters would be ignored.
  • {2,} means you want to find capped words with at least two letters in the specified range (i.e. A-Z). If you only wanted to find two- and three-letter capped words, then you’d change this to {2,3}, and all capped word of four or more letters would be ignored. By not specifying a number after the comma, the ‘find’ will find capped words of any length containing at least two letters.
  • The \1 in the Replace and ‘Highlight’ below that field tells Word to replace what was found with itself, and to highlight it with the selected highlight colour.

Note: This technique does NOT find initialisms separated by periods or any other punctuation; it will find UNICEF but not U.N.I.C.E.F.

 

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.