Word: Wildcard find and replace for numbers and trailing punctuation
July 14, 2015On another post on this blog, Michael commented that he had a situation where he needed to run find and replace routines (possibly using wildcards) on hundreds of documents to convert MANUALLY entered step numbers (with various trailing punctuation) to a common format ready for conversion into another type of document.
He wanted to convert numbered steps like 1), 2), 3), etc. (each on a new line) to (1), (2), (3), etc. (also on separate lines). And convert numbered steps like (1), (2), (3), etc. (each on a new line) to 1., 2., 3., etc. (also on separate lines).
Notes and assumptions:
- These steps ONLY work for manually entered numbered steps, NOT automatic numbering in Word.
- I have assumed that each numbered item starts a new paragraph.
- Before you test this on your document, make a copy of the document and work on that until you are comfortable with what you have to do, and until you are satisfied that these steps are what you want.
- Be very careful with the ‘Replace All’ command — you may inadvertently replace something you don’t want to replace. Only if you’re 100% sure there are no other uses of a number and the relevant trailing punctuation would you use ‘Replace All’. If you’re not sure, use ‘Replace’ only. Yes, this will mean clicking ‘Replace’ for each one, but until you are confident that you won’t replace something you shouldn’t, it’s a safer option.
Example 1: Replace 1) etc. with (1)
- Press Ctrl+H to open the Find and Replace dialog.
- Click More, then select the Use wildcards check box.
- In Find What, type: (^013)([0-9]@)([\)])
- In Replace With, type: \1(\2\3
The three elements of the Find are:
- (^013) — This represents the preceding paragraph marker for the line above the numbered step.
- ([0-9]@) — The [0-9] represents any number from 0 to 9, and the @ represents any number of those numbers, thus not limiting the find to only single digit numbers. This finds numbers like 2, 25, and 283,
- ([\)]) — You need to find a specific character (the closing parenthesis), so you need to enclose it in parentheses. However, because parentheses 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 ).
There are no spaces preceding or trailing any of these elements, or in between them, so if you copy the code from this blog post, PLEASE get rid of any preceding spaces otherwise it won’t work (yes, I know this because it caught me out too!).
The four elements of the Replace are:
- \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
- ( — Tells Word to add an opening parenthesis before the next element (the number).
- \2 — Tells Word to replace the second element of the Find with the same text as what was found (the numerals).
- \3 — Tells Word to replace the third element of the Find with what was in the Find (the closing parenthesis).
Example 2: Replace (1) etc. with 1.
- Press Ctrl+H to open the Find and Replace dialog.
- Click More, then select the Use wildcards check box.
- In Find What, type: (^013)([\(])([0-9]@)(\))
- In Replace With, type: \1\3.
What the find and replace ‘codes’ mean:
The four elements of the Find are:
- (^013) — This represents the preceding paragraph marker for the line above the numbered step.
- ([\(]) — You need to find a specific character (the opening parenthesis), so you need to enclose it in parentheses. However, because parentheses 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 (, AND square brackets surrounding this string (otherwise, it won’t work).
- ([0-9]@) — The [0-9] represents any number from 0 to 9, and the @ represents any number of those numbers, thus not limiting the find to only single digit numbers. This finds numbers like 2, 25, and 283,
- (\)) — You need to find a specific character (the closing parenthesis), so you need to enclose it in parentheses. However, because parentheses 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 ).
There are no spaces preceding or trailing any of these elements, or in between them, so if you copy the code from this blog post, get rid of any preceding spaces otherwise it won’t work .
The three elements of the Replace are:
- \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
- \3 — Tells Word to replace the third element of the Find with the same text as what was found (the numerals).
- . — Tells Word to add a period (full stop) after the third element of the Find.
Michael: I hope this solves your problem. Donations to keeping this blog ad-free gratefully accepted (see the link at the top right of the page).
Update: Michael also wanted to know how to revise these find Strings if the lists used lower case letters — e.g. a), b) or (a), (b), etc. instead of numerals. In that case you’d substitute the [0-9]@ in both examples above with [a-z]. If the letters might include upper case letters, then you’d use [A-z]. Don’t forget to omit the @!
Probably one of the most helpful sites for the ubiquitous but nit-picky WORD find and replace dialog unless you have all the pieces parts memorized which I don’t. I do a lot of pdf conversions which are difficult because sentences are often broken mid line with paragraph marks and numbered lists also give me fits. Actually trying to change anything at the beginning of a sentence/paragraph seems tricky. Current problem is replacing-reformatting a single capital letter on its own line. The current solutions from the web use [^13] are for the start of a new line or paragraph but it also wants to grab and format the preceding paragraph mark. How do you select just the first character on a line and does it matter if is part of a sentence or a paragraph? Also how would you select a paragraph mark that ISN’T preceded by a period? Thanks for the great site.
by Lauren Neville May 11, 2018 at 7:25 amHi Lauren
You can select a standard paragraph mark by using ^p in a standard find and replace (not wildcards). So, if you had multiple (empty) paragraphs one after you could enter ^p^p^p (for 3 paragraphs) and replace with ^p (one paragraph). This doesn’t select the preceding period.
If you had several spaces before a paragraph mark, then you’d type say three spaces, then ^p in the Find, then replace with just ^p.
–Rhonda
by Rhonda May 11, 2018 at 8:45 amI have a question, how to replace the space after numbers with . or –
by Anthony June 28, 2018 at 4:09 pmfor example 1 text with 1. text or 1- text
thanks
Hi, I have written percentage in my report enclosed as (numeric in parenthesis) i.e. i need a wildcard replace code for : (56%) to 56%
Can someone help me, thanks in advance
AVi
by Akash July 21, 2018 at 10:07 pm[…] a comment on another post (https://cybertext.wordpress.com/2015/07/14/word-wildcard-find-and-replace-for-numbers-and-trailing-p…), AVi asked if there was a way to find percentage numbers (e.g. 56%) that were inside parentheses, […]
by Word: Wildcard find and replace for numbers inside parentheses | CyberText Newsletter July 22, 2018 at 7:46 amHi AVi
I’ve written up a solution for you here: https://cybertext.wordpress.com/2018/07/22/word-wildcard-find-and-replace-for-numbers-inside-parentheses/
–Rhonda
by Rhonda July 22, 2018 at 7:48 amI have a paragraph consisting of many paragraphs and multiple lines, I am scanning a block of several lines? How to count the number of lines I have selected? thanks you.
by blu August 18, 2018 at 1:32 amIf you want to search for any letter, either uppercase or lowercase, you have to use
by Paul A. February 23, 2019 at 6:37 am[A-Za-z]
If you [A-z] as you suggested it will find the five characters [ \ ] ^ _ ` that are between the upper and lowercase letters in the range. It’s another Word VBA gotcha.
Thanks Paul A!
–Rhonda
by Rhonda February 23, 2019 at 6:41 amCan you please help me? I am losing my mind with this. I have p1 – p10000. The file was made on an application and p stand for page 1, so there is text between p1 and p2.
by Fatima Muniz May 24, 2019 at 11:17 pmRhonda – This (and your original page I read first) contains really WONDERFUL information! Thank you.
However, I have a question about applying this information to a slightly different situation.
I’ve copied several statutes, each of which is FULL of a variety of different paragraph numbering/lettering levels (not automatic – just text: some are numbers, some are letters, some surrounded parentheses, some without), such as:
“I. “; “A. “; “1. “; “a. “; “(1). “; “(a). “; “(i). ”
Everything in each of these documents is Normal text, but I need these paragraph identifiers to be Bolded. So I’d like to create a macro that uses Find/Replace with wildcards to find all of these various items and BOLD them. (Oh, and the operation must not affect any of the other text in the document nor alter the relative locations of any of the newly bolded characters in the document.)
It seemed like I should be able to base the “Find phrase” on the fact that each instance where I want characters in BOLD type is immediately preceded by a paragraph mark (^13) and each also concludes with a period and two spaces (either with or without parentheses – I’m assuming these parentheses marks would count as “characters” in the Find operation).
I knew I wouldn’t be able to use just a * wildcard in the Find phrase because all of the text in every sentence in these documents would get bolded.
So, I decided to try limiting the number of wildcard characters because it seemed to me fair to assume that the quantity of characters I want bolded in any specific instance should not exceed 6 and I couldn’t offhand imagine an instance where running such a macro would “find” an instance of “a paragraph mark followed by not more than 6 characters, a period and two spaces” that I wouldn’t want Bolded. (OK, I admit this assumption can’t be guaranteed for more than about 98% of such documents, but for that other 2%, I’d be willing to come back and do a manual fix, unless there’s some better way to accomplish what I need done.)
So, between your original approach and the one on this page, I thought I’d be able figure out how to create a macro to:
1. Use a wildcard “Find phrase” to locate every instance of “a paragraph mark (^13) followed by not more than 6 wildcard characters, a period and two spaces”; and
2. Use a Replace phrase to format all of those found characters (including punctuation) as Bold.
I couldn’t do it.
My roadblock was trying to craft the Find wildcard phrase. I looked at many resources, but I just couldn’t find a way to apply a maximum limit to the number of wildcard characters. I found what appears to be a way to specify finding a specific number of wildcard characters by using “^13*{6}. “, but I couldn’t figure out how to cause that syntax to also include instances with only 5, 4, 3, 2, or 1 wildcard characters.
From there, and because I wouldn’t be replacing any of the “found” characters with different characters, I had planned to use just the phrase “\1 ((bold))” in the Replace field (I found that in the article at the MVP link you provided in the earlier article and I believe it just “bolds” all of what was “found”).
So, my question is: Do you know a way to “find” every instance of any number of wildcard characters up to and including a specific maximum count of them? Or can you think of a different approach to accomplish what I need to do (other than just going through the document and changing them all manually)?
Thanks,
by Paul Dalton November 17, 2019 at 7:25 amPaul
Hi Paul
You’re nearly there!
I found a way to do this, based on what I think your pattern is — a number or letter (with or without parens) immediately after a paragraph mark, followed by a period, followed by two spaces. I was able to do the F&R to make it all bold (including the paragraph mark and the two spaces), so if you wanted to remove the bold from the paragraph mark and the two spaces, then you’d need to run a couple more simple F&Rs.
For your wildcard Find, use this: (^13)(*{1,6})(. )
(Note: there are two spaces after the period in the final group; the {1,6} bit from a minimum or 1 to a maximum of 6 characters)
For your Replace, use: \1\2\3
And make the font Bold for the Replace.
This achieves what you want, but those spaces and paragraph marks will also have bold applied, so to get rid of that, use normal F&R and search for ^p (Bold) and replace with ^p (Regular), then (Bold) and replace with (Regular).
–Rhonda
by Rhonda November 17, 2019 at 8:14 amHi Rhonda
by Lynden Lund April 8, 2020 at 8:55 pmHow to I place paragraph marks in front of a number as in bible verses.
There are verses 1-9 and then from 10 upwards. I would like each bible verse to begin on a separate line.
There is also no space between the verse number and the text so i would like a space after the text as well.
I used find ([0-9]) replace \1 space to space single digit numbers then
find ([0-9])([0-9]) replace \1\2 space to put a space after 2 digit numbers
then to remove the space between 2 digits as happened with 1 1 1 2 etc
find ([0-9]) space ([0-9]) replace \1\2
Please help!!
Lynden
Hi Lynden
Can you give me an example of what the text (with numbers) looks like before you started? I’m not sure I understand how it looks now, based just on your description.
Alternatively, you could get the verses already separated onto different lines from the Project Gutenberg King James version: http://www.gutenberg.org/ebooks/10
–Rhonda
by Rhonda April 9, 2020 at 10:34 amOK Lynden, I tried to do what I *think* you wanted to do. It required two wildcard find/replace passes and it should deal with most, if not all, of them. MAKE SURE YOU HAVE ‘USE WILDCARDS’ selected.
The first pass adds a space after a number if it’s immediately followed by a capital letter:
Find: ([0-9])([A-Z]) (there are NO spaces in this string)
Replace: \1 \2 (there’s a space between the 1 and the \2)
The second pass looks for a number following a space, and replace that space with a paragraph mark:
Find: ([0-9]) (type a space BEFORE this string)
Replace: ^p\1 (make sure it’s a lower case ‘p’)
Caveats:
* will not find any verses that don’t start with a capital letter
* will not find any verse numbers that don’t have a space in front of them
* any text that has numerals in it that aren’t verse numbers will also get split onto a new line at the numeral
The instances related to these caveats should be very small, and you can fix those manually as you find them.
I hope this does what you want.
–Rhonda
by Rhonda April 9, 2020 at 10:51 amHow do I search for any 3-digit number in a word document?
by Bets October 7, 2020 at 3:56 pmHi Bets
I’ve written a blog post on how to do that: https://cybertext.wordpress.com/2020/10/08/word-find-3-digit-numbers/
–Rhonda
by Rhonda October 8, 2020 at 6:12 ami would like to replace a paragraph mark which follows a time stamp of the pattern hh:mm^p or hhh:mm^p with hh:mm^t or hhh:mm^t without changing the time stamp just chaging the ^p to ^t but don’t know how to get word to just selectively select and replace those particular paragraph marks and not all paragraph marks
by Malcolm Goodwin September 13, 2021 at 6:49 pmHi Malcolm
To use wildcards to find those ending in a para mark, use this in the Find: ([0-9]{2,})(^013)
To find those ending in a tab, use this: ([0-9]{2,})(^t)
To replace WITHOUT changing the numbers, put \1 in the Replace followed by whatever mark you want (e.g. ^p, ^t etc.)
–Rhonda
by Rhonda September 14, 2021 at 9:17 amHi, How will I code the first or example in VBA? Having trouble with the parentheses and ^013. Anyway these are excellent solutions! Thank you so much. This VBA request is just for further automation. The manual Find Replace works great!
by Selvamany Srinivasan October 30, 2021 at 4:01 pmHi Selvamany
I looked at one of my find/replace macros using wildcards and have copied that here. You’ll need to substitute those find and replace strings with yours, and give the macro a name (where the XXX is) and change the comment to reflect your situation:
Sub XXX()
‘ This macro finds a number range separated by a hyphen
‘ and replaces the hyphen with an en dash
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “([0-9])(-)([0-9])”
.Replacement.Text = “\1^=\3”
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I hope this helps
–Rhonda
by Rhonda October 30, 2021 at 4:17 pmHi, Solved the VBA issue! Thank you
by Selvamany Srinivasan October 30, 2021 at 5:08 pmThanks a million! Rhonda
by Selvamany Srinivasan October 30, 2021 at 7:17 pmHi Rhonda,
Thank you so much for these blog posts, they are extremely helpful and I really appreciate the step-wise explanations.
In your examples the replacement contains the same number as was originally found. Is there a way to adapt your code to perform a calculation on the found number (e.g. -1) and replace with the calculated result? I’m specifically interested in cases where the numbers may not be unique (e.g. 13 may appear twice, and each occurrence needs to be replaced with 12).
I thought I’d found a method (for VBA) using two long-winded arrays in succession, but my tests showed they only replaced the first instance of each number. Thanks in advance for any guidance you can give.
-Nan
by Nan November 4, 2021 at 7:42 pmHi Nan
In all my reading about and understanding of wildcard find/replace, you certainly can’t do what you want that way. Writing a macro may work, but it’s certainly a scenario I’ve never considered and I wouldn’t know how to do that. I suggest you post your problem request to the Microsoft support forums – there are some mighty clever people in there who can do all sorts of things with macros: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word (Note: you used to be able to specify your version of Word and ‘programming’ but those options seem to be gone now.)
–Rhonda
by Rhonda November 5, 2021 at 6:30 amHi Rhonda,
by Cathy S November 27, 2022 at 10:48 amI’ve been bashing my head against a Find & Replace in Word, and no matter what I try, I’m not getting the result that I need. I have a list of instructions, and each set of instructions is preceded by the word “Step” and a number. I want to change the font of both the word “Step” and the number without changing the number itself. I have tried using the Find & Replace function but have been unsuccessful after multiple attempts. Obviously, I’m not getting the proper syntax, so how can I accomplish this task using that function or is there another/better way other than converting all the information manually?
Hi Cathy
This can be done easily with a wildcard F&R. Make sure Use Wildcards is checked, then put this in the Find What field: (Step [0-9])
In the Replace With field: \1
With your cursor still in the Replace field, click Format > Font and chose the font colour, which will be listed below the Replace With field.
Click Find Next, then Replace. Only click Replace All if you are confident that you’ll only change the font of the items you want to change.
The \1 in the Replace field tells Word to replace whatever it found with itself (i.e. make no changes to the text, except the styling changes such as font colour).
–Rhonda
by Rhonda November 27, 2022 at 2:25 pmHi again Cathy – I’ve now written this up as a separate blog post: https://cybertext.wordpress.com/2022/11/27/word-change-font-colour-for-certain-elements-using-find-and-replace/
–Rhonda
by Rhonda November 27, 2022 at 2:41 pmTHANK YOU SOOOOO much! I will bookmark your blog post for the next time (and I’m sure there will be) I forget how it’s done.
by Cathy S November 27, 2022 at 11:33 pmThe explanations of how the various parts of the syntax work will help me (and anyone else) figure out what to do.
by Cathy S November 27, 2022 at 11:36 pmhello…
by venkat narayana February 8, 2023 at 5:35 pmhow to replace only numbers, characters, symbols from font 1 to font 2.
Thanks so much!!
by Burje June 29, 2023 at 10:05 pm