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

 

8 comments

  1. Hi Rhonda,

    Thank you for your wisdom and experience! I’m so close based on your guidance i can almost taste it!

    I’m trying to use the above to simply find sequences of numbers (with at least 6 numbers or more) and change the format to look like they are hyperlinked (blue, underline). To add to the complexity, I also want to grab any number strings that have hyphens.

    Using this in the find: ( )()( ) returns no results in my sample text:

    This is a test of the international broadcasting system 20145554345 and will continue to test the intermediate 2345-45-3456-3232 and any other 2010948 that may or may not 11/24/2018 that will or will not be accurate by $344.09 or any other instance of a 342 that may or may not link to the 4343-9898 or any other version of 2009809887

    What am I missing?

    Fred


  2. Hi Fred

    I got the 6+ numbers thing working pretty much straight away, just by changing the {1,} part of the wildcard Find to {6,}, then replacing with \1\2\3 and setting the formatting of the replace to blue underline.

    This doesn’t find the hyphenated numbers, so I’ll take a look at that next.

    You will likely have to run two find/replace passes to get them all.

    –Rhonda


  3. Thanks, Rhonda. I just replaced with formatting and that worked. However, if I put in the \1\2\3 it returns “That replacement text contains a group number that’s out of range”

    If it works without the \1\2\3 do I need that? What does it do?

    Second, if I have to do two separate Find/Replace to catch both types of numbers, can I create a macro and run both as a function?

    Thank you!

    Fred


  4. Hi Again Fred

    I found a way to do the hyphenated numbers, but it’s certainly not very elegant! Also, if the hyphenated numbers vary in the number of numeral clusters before the end (in your example, the first set of hyphenated numbers has 4 clusters, while the second set has 2), then you may have to vary this and run it several times.

    What I got to work was this for the wildcard find for a 4-cluster set of numbers: ( )()(-)()(-)()(-)()( )

    And for the replace, you’d have to do \1\2\3\4\5\6\7\8\9

    For a 3-cluster set, remove the last set of (-)(), and drop the final number in the replace to \7. You may have to do this several times to cover each cluster of numerals.

    It’s pretty ugly and I’m someone else with more knowledge of this than me could come up with something far more elegant, though it may have to be a macro more so than a find/replace.

    –Rhonda

    Update: Darn! All my carefully typed data inside those parentheses above got stripped by WordPress! What it should be in the 2nd, 4th, 6th, and 8th sets is the same as in Step 3 in the post.


  5. Hi again Fred — if it works fine without the \1\2\3, then leave it out. (yes, I just tested it and that worked fine for me too)

    You would normally use the \1 to tell Word to replace whatever’s in the first set of parentheses with itself (i.e. no change to the content), \2 to replace the content of the second set of parens with itself, etc.

    I’m sure this could be added to macro, but I don;t have the knowledge or skills to do that.

    –Rhonda


  6. Thank you for sharing your time and knowledge; it’s most appreciated!

    Fred


  7. How do I replace a single return with a single space. I have the Find what: as [0-9A-z]^13[0-9A-z] which is working fine, but nothing will work in the Replace with: field. A single space replaces the preceding letter, the return, and the succeeding letter with a single space and every time I enter \1\2 \3 I get a “That replacement text contains a group number that’s our of range. Pulling my hair out as no variation on the Replace with: field works.


  8. Hi Michael

    You might be overthinking this. Instead of using wildcards, you should be able to do this with a standard find and replace. Find ^p and replace with a space.

    Btw, the \1 etc. replace string only works when the elements in the find are enclosed with parentheses, which yours aren’t.

    Hope this helps

    —Rhonda



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: