Posts Tagged ‘wildcards’

h1

Word: Find all upper case words within parentheses and change to lower case

March 6, 2022

One of my readers asked if there was a way to change upper case strings within parentheses to lower case. For example, (INHALED DEEPLY) to (inhaled deeply). It seems they had hundreds of them, so doing them one at a time wasn’t the most efficient use of their time.

I was able to come up with a wildcard find to identify them all, but I couldn’t come up with a replace that preserved the contents while changing the case. I fiddled around a bit trying to get a macro to work, but to no avail. Then I decided to ask my question on the Microsoft support forums, but before I did so, I did a quick search to see if an answer had already been given—and it had! (https://answers.microsoft.com/en-us/msoffice/forum/all/search-and-replace-uppercase-to-lowercase/b2ed6ffa-4fe7-450d-a66c-7578811ce71f)

NOTE: As always, test on a copy of your document before you run this—you are making a global change that may target more than you want. For example, these will all get changed to lower case:

  • upper case acronyms in parentheses
  • anything inside parentheses that starts with a capital letter, such as proper nouns, whether in all upper case or not
  • everything from the first upper case letter after an opening parenthesis to the first-found closing parenthesis, which might be some pages later if you forgot to close the parenthesis.

This is a brute force method and may have unintended consequences. If in doubt, click Find Next to check each instance before you make this global change. 

Steps to achieve this:

  1. Press Ctrl+h to open the Find and Replace window.
  2. Click the Find tab (this is important).
  3. In the Find What field, type \(([A-Z]*)\)
  4. Click More and select the Use wildcards checkbox.
  5. Click Find In and select Main Document from the drop-down list.
  6. All matches are found and automatically selected. DO NOT click inside the document otherwise you will lose the selections.
  7. Go to the Home tab and click the Aa icon in the Font group and change the case to lower case. This will change the case of everything selected.

[Link last checked March 2022]

h1

Word: Italicise numbers inside parentheses

December 2, 2021

In a comment on another blog post, one of my readers asked if there was an easy way to find all numbers and change them to italic. Well, there is (Option 1 below), but there was a further complication—some of these numbers were a range separated by a dash, and some were separated by commas. For example:

  • (123456)
  • (1-4)
  • (1, 5)
  • (1, 5, 12)

I couldn’t find an easy way to do this all at once, but you can do it by running several wildcard find and replace routines. If someone else has an easier or more elegant solution, please add it in the comments.

NOTES:

  • Tip: Just in case anything goes wrong, make a copy of your document before you start and do this in the copy until you are satisfied it works as you expect.
  • Tip: Only click Replace all once you are confident that it works.
  • Numbers are whole positive integers—no decimals, no ordinals, no negative numbers.
  • ‘Dash’ is the standard hyphen character on the keyboard, NOT an en or em dash, or a minus symbol.
  • All find and replace routines are done with Use wildcards turned on (Ctrl+h, click More, select Use wildcards).
  • Tip: Copy the find strings from here and paste into your Word find field.

Option 1: Find all numbers and change to italics

  1. Find: ([0-9])
  2. Replace: \1
  3. When in the Replace field, click Format > Font, and choose Italic.

Note: EVERY number in the document will be italicised.

Option 2: Find whole numbers only inside parentheses and change to italics

For example: (123456), (789), (23)

  1. Find: \(([0-9]@)\)
  2. Replace: ^&
  3. When in the Replace field, click Format > Font, and choose Italic.

Note: The numbers AND their surrounding parentheses will be italicised. See Option 6 for how to change the parentheses back to normal text.

Option 3: Find whole numbers separated with a single dash inside parentheses and change to italics

For example: (12-3456), (7-89), (2-3) [there are NO spaces before or after the dash]

  1. Find: \(([0-9]{1,9})(-)([0-9]{1,9})\)
  2. Replace: ^&
  3. When in the Replace field, click Format > Font, and choose Italic.

Note: The number ranges, the dashes, AND their surrounding parentheses will be italicised. See Option 6 for how to change the dash and the parentheses back to normal text.

Option 4: Find 2 numbers separated by a comma and a space, inside parentheses, and change to italics

For example: (1, 2), (25, 67)

  1. Find: \([0-9]{1,9}, [0-9]{1,9}\)
  2. Replace: ^&
  3. When in the Replace field, click Format > Font, and choose Italic.

Note: The numbers, the comma, the space, AND their surrounding parentheses will be italicised. See Option 6 for how to change the comma, space, and the parentheses back to normal text.

Option 5: Find 3 numbers separated by commas and spaces, inside parentheses, and change to italics

For example: (1, 2, 3), (25, 67, 345)

  1. Find: \(([0-9]{1,9}, [0-9]{1,9}, [0-9]{1,9})\)
  2. Replace: ^&
  3. When in the Replace field, click Format > Font, and choose Italic.

Note: The numbers, comma, spaces, AND their surrounding parentheses will be italicised. See Option 6 for how to change the commas, spaces, and the parentheses back to normal text.

Option 6: Change all commas, spaces, dashes, and parentheses back to normal text

  1. Find: ([, \(\)-]) (Note: there’s a space after the comma in this string)
  2. When in the Find field, click Format > Font, and choose Italic.
  3. Replace: ^&
  4. When in the Replace field, click Format > Font, and choose Not Italic.

 

h1

Word: Replace HTML em markers with italics

November 1, 2021

In a comment on another post, J had this problem: the text file that they’d copied into Word had the HTML code markers for emphasis (em enclosed by the greater than < and less than > arrows)and they wanted to get rid of these markers and change the text between those markers into italics. Unfortunately, WordPress won’t allow me write that code as it wants to automatically convert it into italics!

I solved their problem by using a wildcard find and replace in Word. Here’s how:

  1. Press Ctrl+h to open the Find and Replace window.
  2. Click More.
  3. Select the Use wildcards checkbox.
  4. In the Find field, type this (copy it if you think you’ll get it wrong; there are NO spaces in this string): (\<em\>)(*)(\</em\>) 
  5. In the Replace field, type this: \2
  6. While still in the Replace field, click Format (bottom left of the find/replace window), then Font, then select Italic. Font: Italic should display below the Replace field, as shown in the screenshot.
  7. Click Find Next. if you’re happy with what it found, click Replace. Only click Replace All if you’re confident that every instance of em has a closing em marker nearby.

How this works:

  • Each element of the Find is enclosed by parentheses. There are three elements — the opening em marker (#1), the text in between (#2), and the closing em marker (#3)
  • Because the greater than and less than symbols have special functions in Word’s wildcard find/replace, you have to ‘escape’ them by putting a \ in front of each
  • Because we don’t know which word or words or phrases or numbers are between the em markers, we use * to represent anything between an opening and a closing em marker
  • In the Replace, we don’t need the em markers anymore, so we omit them from the replace. But we do need the words that are enclosed by those em markers, and that’s the second element in the Find, so we type \2 to tell the second thing found to be replaced by itself.
h1

Word: Switch a footnote and following punctuation

June 3, 2021

Someone in an editors’ group on Facebook asked how to change the position of footnotes and punctuation. All their footnotes were followed by a period or a comma, and they wanted to shift the punctuation so that it came before the footnote, not after it.

Word’s find and replace using wildcards to the rescue! Here’s how:

  1. Press Ctrl+h to open the Find and Replace window.
  2. Click More, then select the Use wildcards checkbox.
  3. In the Find what field, type (^2)([.,])
  4. In the Replace with field, type: \2\1
  5. Click Find Next, then Replace for each one found. If you are confident, then click Replace All.

How this works:

  • ^2 is the code for footnotes that can only be used with wildcard searching (for normal searching, you’d use ^f, but you can’t use that in wildcard searches)
  • [.,] defines the range of things to look for after the footnote—in this case, a period or a comma
  • () the parentheses enclose each element of the Find, which means we can shift elements around in the Replace
  • \2\1 in the Replace tells Word to replace by putting the second element found (the punctuation) followed by the first element (the footnote), thus switching the positions

Note: If you just wanted to find only footnotes followed by a period (not a comma), you’d modify the Find to be (^2)(.) 

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: 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.