Posts Tagged ‘find and replace’

h1

Word: Reformat text inside quote marks using wildcards

April 16, 2021

A user on an editors’ Facebook group wanted to know if they could use a wildcard find and replace to reformat (perhaps by using different coloured text, highlighting, bold, italics etc.) the text in between quote marks to distinguish the quotations from other text in the document.

This is an ideal job for using wildcards in Word’s find and replace.

But some warnings apply:

  • There are several types of quote marks—single, double, with straight or curly variations for these, and some people may even type two single quote marks to represent a double quote mark, or use prime and double prime characters to represent a quote mark. The only SURE way to identify the marks used by the author are to copy them from the document and paste them into the Find field.
  • This Find/Replace DOES NOT WORK with single straight quotes—the character used for an apostrophe and to start and end a quotation is the same, so you won’t get the results you expect. Any string of text between one apostrophe and another will also be captured.
  • Make sure the quoted passage has both a starting and ending quote mark. If the end quote mark is missing, the change will occur to ALL text from the beginning quote mark to the next end quote mark found, which could be some pages away.
  • Beware of apostrophes used within a quotation when the quotation is surrounded by single curly quote marks—the Find will find up to the apostrophe, NOT to the ending single curly quote mark. This is because the symbol for an apostrophe and the ending single curly quote mark is the same character.
  • The safest practice is to check what’s found and click Replace if it matches, NOT Replace All.
  • ALWAYS ALWAYS ALWAYS test this on a copy of the document before you use it on the original document.

So, if you’re using double quotes (straight or curly), or single curly quotes, you can use this Find/Replace. I explain what the settings mean after these steps, if you’re interested. Meantime, here’s my solution, which works in all versions of Word:

  1. If you want to identify the quoted sections with a highlight colour, choose it first. Ignore this step if you want to change the font colour or styling.
  2. Select the text you want to change (e.g. entire document, selected paragraphs, selected columns or rows of a table).
  3. Press Ctrl+H to open the Find and Replace dialog box.
  4. Click the More button.
  5. Select the Use wildcards check box.
  6. Put your cursor into the Find what field—what you do next depends on the type of quote mark used in the document:
    • Straight double quotes: type the quote mark, followed immediately by an asterisk, then another quote mark.
    • Curly quotes (single or double): copy an opening quote mark from the document and paste it into the Find field, then type an asterisk immediately after it, then copy/paste the ending quote mark immediately after the asterisk.
  7. In the Replace with field, type: ^&
  8. Click the Format button.
  9. If you want to apply highlighting to the found text, select Highlight. If you want to apply character formatting (colour, bold, italics, etc.), select Font, select the character styling you want, then click OK.
  10. Your Find and Replace dialog box should look something like this, with the highlighting or character styling choice shown below the Replace with field:
  11. Click Find. Check the text found is what you expect—if so, click Replace, then click Find Next. Avoid clicking Replace All unless you are absolutely certain all quotes have a starting and ending quote mark and that there are no apostrophes within a quote.

What it all means

The quotes in the Find are self-explanatory. The asterisk between them says to find any number of characters (including spaces, punctuation marks, letters, numbers, etc.) between the first quote mark found and the next one found. NOTE: If the find/replace doesn’t match anything, check the type of quote marks you’re using and make sure you copy/paste the opening and closing ones into their correct position in the Find.

The ^& in the Replace says to replace whatever is found with itself (in other words, make no changes to the characters), and the font styling/highlighting below the Replace field tells word to make the replaced text that colour or style.

See also:

[Links last checked April 2021]

h1

Word: Find a special character written as text

February 15, 2021

When you want to find a special character, such as a paragraph mark, in Microsoft Word, you have to use a special character (in this case, ^p to search for all paragraph marks). And for most situations this works as you’d expect.

But if you’ve written ^p as text in a document, as I did for a document I’m writing on Word’s find and replace functions, where I need to type the characters used to find the special character, then you can’t search for that text string by typing ^p as the search term. Instead of finding what you wrote, you get results that list ALL paragraph marks in the document, and nothing matching what you wrote.

I tried all sorts of things with wildcards, escape characters etc. before asking a specialist editors’ group on Facebook, where one of the members pointed me in the right direction—you have to add another caret before the ^p, so ^^p.

Easy when you know how!

h1

Word: Add parentheses around a footnote number

November 20, 2020

Warning: I don’t use footnotes, and my authors rarely use them so footnotes and endnotes are not something I’m familiar with in Word.

M contacted me and asked how he could change the footnote numbers in his dissertation from unadorned superscripted numbers to those same numbers but with parentheses around them. He’d done some testing and had found a Quora post (https://www.quora.com/How-can-I-remove-all-the-brackets-around-the-numbering-of-the-footnotes-in-a-Word-Document) that detailed how to get rid of them, but couldn’t figure out how to add them automatically. Doing it manually in the body of the document and in the footnote area wasn’t going to work as there were hundreds of them.

The Quora post alerted me to something I wasn’t aware of—there’s a special wildcard symbol for footnotes: ^2. Armed with that information, I opened a new Word document, added some text and inserted some footnotes into it. Then I did a find and replace using wildcards and was able to automatically insert parentheses around all the superscripted footnotes, both in the body of the document and in the footnote area.

Wildcards must be on and you must have the cursor in the main body of the document, NOT the footnotes area, then:

  • Find: (^2)
  • Replace: (\1)

What this does is find a footnote number (the ^2 bit), but so that I could reference that in the replace, I surrounded it in parentheses, thus (^2).

The replace adds an opening and closing parenthesis either side of the found number (that’s the \1 bit).

NOTES:

  • You should only run this once, once you have all footnotes in place. If there are already parentheses around some footnote numbers, you will end up with two parentheses.
  • Any new footnotes you add will NOT have the parentheses. This does NOT change how Word auto numbers footnotes.
h1

Word: Find 3-digit numbers

October 8, 2020

A reader wanted to know how to find the 3-digit numbers in their document.

Using wildcards, that sounds easy enough, but the issue is that my initial attempt (find: ([0-9]{3}) ) found ALL strings of three numbers, not just the whole numbers that have ONLY three numbers. So, in a number like 12345, it found 123, then 234, then 345.

I needed to add more commands to force it to ignore longer numbers and numbers containing punctuation (e.g. decimal numbers and thousand separators). The only way to do that was to make it look for a space followed by a 3-digit number, followed by a space or any other ending punctuation (comma, period, colon, semicolon, question marks, exclamation point, closing parenthesis, closing square bracket should cover most variations).

Note: I use Word for Windows—the commands may be a little different in Word for Mac.

Instructions

  1. Press Ctrl+h to open the Find and Replace window.
  2. Click the Find tab.
  3. Click More to open the lower section of the window.
  4. Select the Use wildcards checkbox.
  5. In the Find what field, type: ( )([0-9]{3})([ .,;:\)\]\?\!])  (you may find it easier to copy this then paste it into the Find field; Note: the first set of parentheses contains a space, and the third set of parentheses has a space immediately after the opening square bracket).
  6. Click Find Next.

How this works

  • ( ) looks for a space 
  • ([0-9]) looks for any number from 0 to 9 immediately following the space, and {3} looks for any string of three numbers
  • ([ .,;:\)\]\?\!]) looks for a range (that’s the square brackets immediately inside the parentheses) containing any of these: a space, period, comma, semicolon, colon, then punctuation symbols that have to be ‘escaped’ because they are also special commands in wildcard searches—these are each preceded by a backward slash (the ‘escape’ character) and are a closing parenthesis, a closing square bracket, a question mark, and an exclamation point.
h1

Word: Transpose punctuation characters at the end of a quotation mark

September 26, 2020

In the main, British English and American English treat punctuation at the end of speech quotation marks differently—US English has the punctuation inside the final quotation mark, whereas British English has it outside (there may be some exceptions to this general rule, but let’s just go with what usually happens).

If you need to style speech from British English to US English, you need to find all the various punctuation marks that come after a quotation mark and transpose them so they come before the quotation mark. You can do with with a find and replace using wildcards.

These instructions are for Word for Windows, but you should be able to do something similar in Word for Mac.

  1. Before you start, copy an ending quotation mark to the clipboard. Why? Because there are various symbols used for quotation marks (straight, curly, even a single or double prime) and you want to make sure you get the one you use.
  2. Press Ctrl+h to open the Find and Replace window.
  3. Click More.
  4. Check the Use wildcards checkbox.
  5. In the Find what field, type: (”)([.,;:\?\!]) (Note: In the first set of parentheses PASTE the quotation mark you copied at Step 1.)
  6. In the Replace with field, type: \2\1\
  7. Click Find Next, then Replace on each that you want to transpose. Only click Replace All once you are confident that you won’t inadvertently change something you shouldn’t.

How this works:

  • In the Find, you have two elements, each surrounded by a set of parentheses—these are elements 1 and 2.
  • In the first element, you paste the quotation mark and make sure it is surrounded by opening and closing parentheses.
  • In the second element, you also have opening and closing parentheses, then within those you have a set of opening and closing square brackets—these indicate that you are looking for a range of characters, and the characters you’re looking for in this instance are a period, a comma, a semicolon, a full colon, a question mark, and an exclamation point. The question mark and exclamation point are special characters in a wildcard Find so you must indicate you want the actual character and not the special function it has. You do this by ‘escaping’ each of these characters with a backslash.
  • In the Replace, you want to position element 2 immediately before element 1 (i.e. transpose it), with no spaces or anything else, which is represented by \2\1\.
h1

Word: Changing the punctuation before and after a page number

September 5, 2020

Jeremy wanted to change how his page numbers were punctuated. He needed to change instances such as ‘p152’, ‘p24’ etc. to ‘-p. 152.’, ‘-p. 24.’—that is, he wanted to add a dash in front of the ‘p’, a period and a space after the ‘p’, and a period at the end of the number. I didn’t know what his number range extended to, so I created the Find string to include any of one to four numbers.

This is an easy thing to do using Word’s wildcard find and replace, but there are some warnings:

  • where you have p123-p125, you’ll get ‘-p. 123.–p. 125.’ (i.e. with two dashes—these can be easily found and replaced later using a standard find/replace)
  • where you have p123-125, only the first instance will be replaced as it’s the only one with the ‘p’—you’ll get ‘-p. 123.-125’ (again, you can find <period><dash> later and replace it)
  • I use Word for Windows—the commands may be a little different in Word for Mac.

Instructions

  1. Press Ctrl+h to open the Find and Replace window.
  2. Click More to open the lower section of the window.
  3. Select the Use wildcards checkbox.
  4. In the Find what field, type: (p)([0-9]{1,4})  (you may find it easier to copy this then paste it into the Find field).
  5. In the Replace with field, type: -\1. \2. (there’s a space after the first period).
  6. Click Find Next.
  7. Check that the item found is correct (i.e. matches the pattern of ‘p’ following immediately by one or more numbers), then click Replace.
  8. If you are very confident that this works for you, you can click Replace All, BUT with Replace All you don’t get the opportunity to manually correct variations, such as those listed in the warnings above.

How this works

  • (p) looks for the letter ‘p’ (this is the first element)
  • ([0-9]) looks for any number from 0 to 9 immediately following the ‘p’, and {1,4} looks for any length of number from one character to four (e.g. p5, p23, p123, p1234)—if the numbers are larger than this, increase the ‘4’ in this part, to a larger number (this is the second element)
  • adds a dash in front of \1, which represents the first element in the Find (i.e. the ‘p’)
  • . \2. adds a period after the first element, then a space, then the second element (which is the one to four digit number), then another period.
h1

Word: Shifting one set of information to a another place in the same paragraph

August 20, 2020

In one of my Facebook editors’ groups, SB had this dilemma: “[I have a big Word document] listing moth species. Each species entry starts with the species ID number and ends with the old species ID number, which is in brackets. Is there a macro/wildcard I can use to move the final ID number so that it follows the initial one? Both ID numbers vary for each species. Here’s an example:

49.204 Species name 5.5mm Nb. May–Jul. In woods and heaths. Flies in afternoon sun and to light. FP Between spun leaves of Buckthorn or Alder Buckthorn. (1122)

I would like to move the ‘(1122)’ so that it appears immediately after ‘49.204’. There are literally hundreds of species entries and both numbers change for each species.”

What SB wanted was: 49.204 (1122) Species name 5.5mm Nb. May–Jul. In woods and heaths. Flies in afternoon sun and to light. FP Between spun leaves of Buckthorn or Alder Buckthorn.

This one is tailor-made for a wildcard find and replace, but some assumptions must be made first:

  • Assumption 1: The pattern for the current ID number (2 numbers, period, 3 numbers) is consistent
  • Assumption 2: The only parentheses in the paragraph are the ones enclosing the previous ID number
  • Assumption 3: Only numerals are used inside the parentheses. The number of numerals is unimportant (up to 15) but there should be no spaces, no punctuation or other symbols, and no letters within the parentheses.

If all these assumptions are met, then you can use this wildcard find/replace in Word. The usual caveats apply: Test it on a COPY of your document first, and copy/paste the code from this blog post to avoid errors.

  1. Press Ctrl+h to open the Find and Replace window.
  2. Click More to show the checkbox options.
  3. Select the Use wildcards checkbox.
  4. In the Find field, copy this: ([0-9][0-9])(.)([0-9][0-9][0-9])(*)(\([0-9]{2,15}\)) (There are NO spaces in this string)
  5. In the Replace field, copy this: \1\2\3 \5\4 (Note: There’s only ONE space in this strong, between \3 and \5)
  6. Click Find Next, then Replace.
  7. Assuming it worked as you expected, repeat Step 6 as many times as needed. If you are confident that it does what you want it to do, then click Replace All.

How this works:

  • ([0-9][0-9]): This first element (each separate element is enclosed with parentheses) of the Find looks for a 2-digit number. This first element will become \1 in the Replace.
  • (.): This second element looks for a period (this will become \2 in the Replace). Because it is immediately after the first element, that means that Word will look for two numbers immediately followed by a period.
  • ([0-9][0-9][0-9]): This third element (\3 in the Replace) looks for a 3-digit number. Because it is immediately after the second element, Word will look for a period immediately followed by three numbers.
  • (*): This fourth element (\4 in the Replace) looks for anything—any number of characters.
  • (\([0-9]{2,15}\)): This fifth element (\5 in the Replace) is a bit tricky. You start by opening a new element with an opening parenthesis (as for the others above), but you now want to find an opening parenthesis, but you can’t just type it in because a parenthesis is a special character in a wildcard find/replace. You have to ‘escape’ it by typing \ in front of the opening and the closing parentheses of the string you want to find. The rest of this element comprises any number (the [0-9] part) that’s between 2 and 15 characters long (the {2,15} part.
  • In the Replace, you rearrange the elements into the order you want them, and add any spaces or other punctuation you need. So, \1\2\3 \5\4 means that the first three elements remain the same as they were (in the example, that’s 49.204), then there’s a space, then the fifth element (in the example, that’s (1122)), then all the characters that make up the fourth element.

I’m sure this find/replace string could be improved, but it was what I came up with in a few minutes. And it solved SB’s problem and saved her a LOT of time as she had hundreds of these to do.

 

h1

Word: Format text marked with exclamation points as bold

July 18, 2020

One of my readers had a problem. Their author had marked up words they wanted as bold with exclamation points. For example, ‘make !this text! bold, and !this! too’.

You can use a simple wildcard find and replace to find all such terms, delete the exclamation points, and make the found text bold.

  1. Press Ctrl+h to open the Find and Replace window. Make sure you’re on the Replace tab.
  2. Click More, then select the Use wildcards checkbox.
  3. In the Find field, type: (\!)(*)(\!)
  4. In the Replace field, type: \2
  5. With the cursor still in the Replace field, click Format > Font, and then select Bold from the Font Style list.
  6. Click Find Next.
  7. Assuming the text string found is what you want, click Replace.
  8. Repeat Steps 6 and 7 until all such strings are found and replaced. If you are confident that nothing else will get replaced, click Replace All, but be careful—if the author has missed a beginning exclamation mark, the wrong text will have bold applied to it.

Here’s how the find/replace works:

  • (\!): An exclamation point is a special character in wildcards, so to treat it as an exclamation point and not as the special character, you have to ‘escape’ the character by adding a \ before it. The parentheses enclose this element as you need to eliminate this exclamation point in the replace. There are two of these—one at the beginning (position 1) and end (position 3) of the Find string.
  • (*): Finds anything between the first element (the first exclamation point) and the next element (also an exclamation point). This is position 2 in the Find string.
  • \2: We want to replace what was found with just the text between the exclamation points, and ignore (delete) the exclamation points, so we put in \2 to tell word just to replace whatever you found at position 2 with itself.
  • By clicking Format > Font and selecting Bold, we tell Word to make whatever you replaced bold; this is shown by Font: Bold below the Replace with field.

 

h1

Word: Macro to find and highlight a set of specified words

February 22, 2020

This is a simpler version of a previous blog post—it doesn’t require you to set up and store a separate file of the words you want to highlight.

I wanted to create a list of vague words (such as some, most, often, frequently, sometimes—words that aren’t explicit or specific enough in technical writing and editing) that a simple Microsoft Word macro could find and highlight for me. I found some macros online that each partly solved the problem, and adapted one from Macropod (Paul Edstein) to suit my purposes as it was the closest to what I wanted. Full thanks to Macropod for this one.

There are several ways you can adapt this macro—I’ve commented some of my ideas in the code. I think the words are case sensitive, so if you want one with a capital (e.g. Some), you should add it as another entry.

NOTE: Copy all the code below to the clipboard—it goes off the page, so don’t type it out as you’ll miss some of it or could make a typo—then paste it into your VBA window.

Sub VagueWords()

' Source: Paul Edstein (Macropod), 8 Aug 2015: https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-search-and-replace-multiple-wordsletters-in/af4753a0-7afd-433b-910d-a148da66f2bf
' Original macro name: MultiReplace
' Adapted by Rhonda Bracey, Cybertext Consulting, 22 Feb 2020
' You could duplicate this macro with a different name (e.g. LegalWords [for must, shall, etc.]) using a different list of words in the StrFind and StrRepl lists

Dim StrFind As String
Dim StrRepl As String
Dim i As Long

' In StrFind and StrRepl, add words between the quote marks, separate with a comma, NO spaces
' To only highlight the found words (i.e. not replace with other words), either use StrRepl = StrFind OR use the SAME words in the same order in the StrRepl list as for the StrFind list; comment/uncomment to reflect the one you're using
' To replace a word with another and highlight it, put the new word in the StrRepl list in the SAME position as the word in the StrFind list you want to replace; comment/uncomment to reflect the one you're using

StrFind = "very,just,rarely,often,frequently,majority,most,minority,some,perhaps,maybe,regularly,sometimes,occasionally,best,worst,worse,better,seldom,few,many"
StrRepl = StrFind
' StrRepl = "very,just,rarely,often,frequently,majority,most,minority,some,perhaps,maybe,regularly,sometimes,occasionally,best,worst,worse,better,seldom,few,many"
Set RngTxt = Selection.Range

' Set highlight color - options are listed here: https://docs.microsoft.com/en-us/office/vba/api/word.wdcolorindex
' main ones are wdYellow, wdTurquoise, wdBrightGreen, wdPink
Options.DefaultHighlightColorIndex = wdTurquoise

Selection.HomeKey wdStory

' Clear existing formatting and settings in Find and Replace fields
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

With ActiveDocument.Content.Find
  .Format = True
  .MatchWholeWord = True
  .MatchAllWordForms = False
  .MatchWildcards = False
  .Wrap = wdFindContinue
  .Forward = True
  For i = 0 To UBound(Split(StrFind, ","))
    .Text = Split(StrFind, ",")(i)
    .Replacement.Highlight = True
    .Replacement.Text = Split(StrRepl, ",")(i)
    .Execute Replace:=wdReplaceAll
  Next i
End With
End Sub

For another way to do this, but instead changing the colour of a selected set of words, see https://wordribbon.tips.net/T013773_Changing_the_Color_of_a_List_of_Words.html

Update Oct 2020: A professor has made a YouTube video for his students/colleagues of this in action: https://www.youtube.com/watch?v=OIg95ETFxMQ

[Links last checked June 2020]

h1

Word: Find whole numbers inside square brackets or parentheses

January 29, 2020

In the comments on my post on using wildcards to replace and reformat text inside square brackets, Simon J asked what he’d need to use to find all numbers inside brackets.

I can think of a scenario where I’d want to do what Simon is asking—some of my authors, usually from an engineering background, will write out a number in full then put the numeral in brackets after the word (e.g. seventeen (17) joists, five (5) days). I usually delete one of these when I’m editing, as there’s typically no reason to have both.

Find what field:

  • To find numbers inside square brackets, make sure Use wildcards is on, then type this in the Find what field: \[[0-9]{1,10}\] 
  • To find numbers inside parentheses (round brackets), make sure Use wildcards is on, then type this in the Find what field: \([0-9]{1,10}\) 

Replace with field:

  • To replace what you found with nothing (i.e. delete all such occurrences), then leave the Replace field empty. Note: You may end up with double spaces (the spaces either side of the deleted bracketed number string), so do a standard find and replace to replace them with a single space.
  • To replace what you found with something else, then add that to the Replace field.

Notes and warnings:

  • This method only finds WHOLE numbers. Numbers with anything else in them (letters or any sort of punctuation, such as decimal points, any sort of hyphen or dash, commas as thousand separators, percent symbols etc.) will NOT be found.
  • You can change the parameters of the search string—the example above looks for one to ten consecutive numerals inside the brackets (this is the {1,10} bit). If you wanted different parameters, then you can change these values. For example, {2,5} would look for any whole numbers with 2, 3, 4, or 5 numerals (it would ignore single numbers and numbers with 6 or more numerals), and {1,} would look for numbers of any length from single-digit numbers through to an unlimited string of numbers.
  • This method does not allow you to replace just parts of the found string with other characters. For that you would need to surround each element in parentheses. For the first example, you’d use (\[)([0-9]{1,10})(\]), and for the second you’d normally use ([\(])([0-9]{1,10})([\)]), (Hint: Copy this string—there are lot of punctuation marks in here that you could get wrong if you try to type is out).

[Links last checked January 2020]