• MS Word 2016 Find & Replace Function

    • This topic has 17 replies, 5 voices, and was last updated 6 months ago.
    Author
    Topic
    #2721554

    Hello. I have a document with numerical data. I need to replace all numbers less than 5 such as 1,2,3,4 with <5 as part of redacting. When I find and replace , for example,1, it is also picking up 1,000.  For the number 1 , it becomes <5 , which is correct. But 1,000 also get caught in the find & replace and became <5,000. It should remain as 1,000.

    How can I replace just  the number  1  with <5  and not 1,000? Any advice to help me out  greatly appreciated.  Thank you.

    Viewing 10 reply threads
    Author
    Replies
    • #2721589

      MS Office is not great with that. You will need to write a macro script or VBA for it. It would look for values > 5, than replace with what you want.

       

      Other option, use Open Office which has better tools for that. Plus it is free to use.

       

    • #2721628

      1. Open Find and Replace: Press Ctrl + H to open the Find and Replace dialog box.

      2. Enable Wildcards: Click on More >> to expand the options, then check the box that says Use wildcards.

      3. Find the Numbers:

      To find the number 1, enter ([!0-9,.])1([!0-9,.]) in the Find what: field. This searches for the number 1 that isn’t part of a larger number.

      Similarly, you can do this for 2, 3, and 4.

      4. Replace the Numbers:

      In the Replace with: field, enter \1<5\2 for each corresponding number. This ensures that only standalone occurrences are replaced and not parts of larger numbers.

      5. Replace All: Click on Replace All.

      Here is a summary of the Find what: patterns for each number:

      For 1: ([!0-9,.])1([!0-9,.])

      For 2: ([!0-9,.])2([!0-9,.])

      For 3: ([!0-9,.])3([!0-9,.])

      For 4: ([!0-9,.])4([!0-9,.])

      And the Replace with: pattern:

      For each number: \1<5\2

      This method should ensure that only the numbers 1, 2, 3, and 4 that stand alone are replaced with <5 without affecting larger numbers.

      1 user thanked author for this post.
    • #2721630

      How can I replace just  the number  1  with <5  and not 1,000? Any advice to help me out  greatly appreciated.  Thank you.

      Here is how to find and replace single digit numbers 1, 2, 3,or 4 with <5 with a single code.

      Open Find and Replace (Ctrl + H)
      Click the more and click to Use Wildcards
      In the Find box enter:

      <[1-4]{1}>

      In the replace with

      <5

      Click Replace all and all single digit numbers 1-4 will be replaced with <5.

      For future reference the numbers in brackets tell what numbers to look for and the {1} tells how many digits the number must have. Thus only 1 digit numbers containing the numbers you are looking for are found.

      HTH, Dana:))

      • #2721633

        Doesn’t seem to cope with 1,000.

    • #2721677

      Doesn’t seem to cope with 1,000.

      Correct.
      Thank you for pointing that out. I wrote that code so quickly I didn’t think of the numbers with commas. The code needs disqualifiers to not find numbers with a comma after it as well as numbers after it which will make it a double digit number.

      Here is the code to go into the Find box:

      <[1-4]{1}[!,0-9]

      Note the above code does not have the > at the end of the code like the first code did. This is correct and do not add that > to the above code.

      The above code will not find 1,000 or 1000 and will find only 1, 2, 3, or 4.

      Note the above code is for whole numbers. If decimal digits are used then that code will find 0.1 but not 0.10.

      HTH, Dana:))

      • #2721701

        Hi thank you for your code. Tried it and although it did not convert 1,000 to <5 , it combined the results of some 2 and 3 and returned one <5 instead of 2 individual <5.

        the previous solution below did not work either

         

        For 1: ([!0-9,.])1([!0-9,.])

        For 2: ([!0-9,.])2([!0-9,.])

        For 3: ([!0-9,.])3([!0-9,.])

        For 4: ([!0-9,.])4([!0-9,.])

        And the Replace with: pattern:

        For each number: \1<5\

        • #2722125

          Finding with  ([!0-9,.])1(!0-9,.]) the massage was “we made 0 replacements in your selection.” It failed to make any required replacements.

    • #2721708

      Tried it and although it did not convert 1,000 to <5 , it combined the results of some 2 and 3 and returned one <5 instead of 2 individual <5.

      Could you post some examples of the numbers that did not replace right. I need to see what is in the spaces before these numbers and the spaces after these numbers exactly how it appears in the document. Are these numbers in a table or any characters attached such as $1 or 1.? If I can see what caused the wrong find I can adjust the code.

      HTH, Dana:))

    • #2721838

      Hello. My data is copied over from Excel into a word document. There are no extra spaces in the table containing the numbers

      Notwithstanding, i trialled the suggested codes in a table in Word

      I made a 3 by 3  table and inserted into the cells the following numbers:

      1 2 3 4 5  10  100 and 1,000

      The results are:

      With <[1-4]{1}> code  , it replaced the all the 1 to 4 with <5  including the 1,000 which became <5,000. It left the 5 10 100 alone.

      With <[1-4]{1}  code without the > bracket, it replaced any digit 1-4, for example, 1 as <5, 4 as <5 , 100 as <500 and the 1,000 as <5,000  – I tried a 7 and interestingly it did not detect the 7  and left it alone

      With <1-4]{1}[!,0-9]  code,  the message was *All done – we made 0 replacements*

      Thank you for any guidance on this.

    • #2722233

      Hello. My data is copied over from Excel into a word document. There are no extra spaces in the table containing the numbers Notwithstanding, i trialled the suggested codes in a table in Word I made a 3 by 3  table and inserted into the cells the following numbers:

      I thought so. The disqualifier codes will not work for numbers in a table, however there is a workaround for this that requires only a couple extra steps to get what you want. You should try this out on your test 3 x 3 table.

      1) Select the table so the entire table is highlighted as selected.
      2) On the Table Layout tab in the Data section select Convert to text which will open the Convert box as shown in the image below.

      Covert-table

      3) Select Other: which has the dash (-) and click OK. The table lines will disappear leaving the numbers and the dash (-) dividers and all the lines of numbers are still highlighted.
      4) Press Ctrl + H to open Find and Replace.
      5) In the Find box enter:

      <[1-4]{1}[!,0-9]

      6) In the Replace box enter:

      <5-
      Note the -after the 5
      7) Click Replace all. Word will report the number replaced and ask to do the rest of the document. Click NO and all the numbers are still highlighted.
      8) On the Insert tab above in the Tables group select Convert Text to Table which opens the Convert Text to Table window as shown below:

      Text-to-table

      9) In the Convert Text to Table box make sure the number of columns matches the number in the original table. Also make sure Other: – is selected and click OK. Note: A number that is replaced with the <5- in the last column to the right will try to add an extra column. Keeping the number of columns will keep the data in the correct order but adds an extra blank row (which you can delete).

      You will now be back to your table with all the numbers changed as you wanted.
      Try it out and let me know if you need more help.

      HTH, Dana:))

      • #2722334

        Hi. I got as far as converting to text.

        with the numbers 1 2 5 1010 1,000 -and 100

        the converting to text looked like

        1-2

        5-1010

        1,000-100

        When I tried the code, it returned the message “We made 0 replacements in your selection”.

    • #2722372

      When I tried the code, it returned the message “We made 0 replacements in your selection”.

      I should have reminded you. That code requires the Use Wildcards option checked in the Find and Replace window (Click the More button to see it). Once the Use wildcards is selected the code will run and from your test table will change 2 numbers. The results will look like below:
      <5-<5-5-1010
      1,000-100
      The numbers will change which line they were on. Don’t worry about this. Highlight all the numbers and select Insert> Convert Text to Table. In the Convert Text to Table window be sure to enter the number of columns the original table had and in this case was 2. I wrote the code this way to avoid other problems with numbers in a table. I also check Autofit to keep the table from spreading across the page. When you click OK the table will return as it was as the screenshot below:

      Converted-Table

      Let me know if you need more info or help.

      HTH, Dana:))

      • #2722379

        Thank you for your help.

        I came across this expression in one of the REGEX sites.

        ^[1-4]$

        My question is – what does ^ do?

        in the Regex tester site, ^[1-5]$ and [1-5]$ ie with and without the ^ works.

    • #2722442

      I came across this expression in one of the REGEX sites. ^[1-4]$ My question is – what does ^ do? in the Regex tester site, ^[1-5]$ and [1-5]$ ie with and without the ^ works.

      The ^ (caret) is used with special characters to find or replace specific things. I don’t see how that code can work since the caret (^) can only be used with special characters and can’t be used with a range bracket ([]). Also the $ is the special character for any letter (^$ finds any letter, ^# find any number) and has nothing to do with numbers. Also the caret can’t be used with wildcards. Just for grins I tried that code both with table and without and it does not find any numbers at all.

      The caret is a very powerful Find and Replace code that can be used to replace a word of text with a photo or multiple pages (book) into the document, but it can’t be used when trying to find a range of either characters or numbers.

      Did you finish testing my code on your test table with the Use Wildcard option? Did you have problems with that method?

      HTH, Dana:))

    • #2722481

      I came across this expression

      In regex the caret (^) means “the search item must be found at the start of the line”.

      e.g. This is some text
      Find: ^some
      Nothing will be found since the word “some” is not at the start of the line.

      From Dana’s description it seems the Word uses caret in a different way, so it’s not a regex search.

      cheers, Paul

      p.s. when I’m working on regex queries I use this site.

    • #2722570

      In regex the caret (^) means “the search item must be found at the start of the line”. e.g. This is some text Find: ^some Nothing will be found since the word “some” is not at the start of the line. From Dana’s description it seems the Word uses caret in a different way, so it’s not a regex search. cheers, Paul

      I never used regex. In Word the first of a word or character is selected with < code, but I don’t know of any code for first word on a line. How do you run the regex codes in a Word document?

      In Word the caret (^) does this:
      ^? – Finds any character, including letters, numbers, punctuation, spaces, special characters, and formatting marks.
      ^# – Finds any number
      ^$ – Finds any letter
      ^g – Finds any picture or graphic, but will only find it when such picture has Text Wrapping set to In Line with Text
      ^f – Finds any footnote mark
      ^w – Finds any space or blank spots (group of spaces) – This is useful sometimes as the size of the space will be shown highlighted.
      and the biggest is ^c which in the Replace box will replace the last thing copied to the clipboard (AKA last thing copied) which can be multiple pages of text or a photo or graphic.

      It appears that regex coding is totally different from Word coding.

      HTH, Dana:))

      • #2722733

        After reviewing what Office 123 posted, there is a much easier way to get what he wanted.
        It is very easy to change 1-4 to <5 in Excel with the IF formula and then import the Excel data into the Word document. You can even embed the Excel worksheet so it looks like a Word table.

        HTH, Dana:))

    Viewing 10 reply threads
    Reply To: MS Word 2016 Find & Replace Function

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: