Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-03-2022, 03:02 PM
BrianHoard BrianHoard is offline How to remove square brackets surrounding numbers with commas Windows 10 How to remove square brackets surrounding numbers with commas Office 2019
Advanced Beginner
How to remove square brackets surrounding numbers with commas
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default How to remove square brackets surrounding numbers with commas

We have built a script to remove square brackets surrounding numbers, like [2] or [99999], then making the numbers formerly wrapped in square brackets to superscript.

But today, we found we also must remove square brackets from comma-separated numbers, like [2,3] or [2,3,4,5,6,7,8,9,10]



I'm trying to do wildcards in find, like this, but this only works for patterns with a single comma.

In my .Find.Text attempt, here's what I'm trying to do...
- Find a literal open square bracket,
- followed by any number, 1 or more times,
- group start
- followed by a literal comma, 0 or one times,
- followed by any number 1 or more times
- group end
- the above group 0 or more times,
- ending with a close square bracket]

Code:
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Text = "\[[0-9]{1,0}(\,[0-9]{1,0}){,}\]"
    .Replacement.Text = ""
  End With

  Do While .Find.Execute = True
    str_found = .Text
    str_found = Replace(str_found, "[", "") ' replace open [ bracket with nothing.
    str_found = Replace(str_found, "]", "") 'replace close ] bracket with nothing
    .Font.Superscript = str_found
    .Text = str_found ' Replace the text in doc, now without square [] brackets.
    .Collapse wdCollapseEnd
  Loop
End With
Reply With Quote
  #2  
Old 10-03-2022, 03:13 PM
macropod's Avatar
macropod macropod is offline How to remove square brackets surrounding numbers with commas Windows 10 How to remove square brackets surrounding numbers with commas Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You don't need a macro for this - much less one with a loop. All you need is a wildcard Find/Replace, where:
Find = \[([0-9,]@)\]
Replace = \1
with the replacement font attribute set to superscript.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-03-2022, 03:29 PM
BrianHoard BrianHoard is offline How to remove square brackets surrounding numbers with commas Windows 10 How to remove square brackets surrounding numbers with commas Office 2019
Advanced Beginner
How to remove square brackets surrounding numbers with commas
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Wow Macropod! You are amazing! This is working so far, so good. We're running it on our docs now.
Reply With Quote
  #4  
Old 10-03-2022, 03:45 PM
BrianHoard BrianHoard is offline How to remove square brackets surrounding numbers with commas Windows 10 How to remove square brackets surrounding numbers with commas Office 2019
Advanced Beginner
How to remove square brackets surrounding numbers with commas
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Find = \[([0-9,]@)\]
Replace = \1

If I'm understanding how you did this, character-by-character...
\[ ' literal open bracket
( ' beginning of a group?
[0-9] ' any single digit, 0-9,
1 or more times,
, ' Not sure how it knows to use the comma 0 or more times? But that's what it's doing.
) ' end group
@ look for the pattern [0-9], 1 or more times?
\] ' literal close bracket.

Replace = \1
What does this mean? It is removing the square brackets, but how does it know that?
I'm trying to understand how this all works as we may also want to allow for periods in the brackets, like [2,3.4]
Reply With Quote
  #5  
Old 10-03-2022, 06:32 PM
Guessed's Avatar
Guessed Guessed is offline How to remove square brackets surrounding numbers with commas Windows 10 How to remove square brackets surrounding numbers with commas Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

() are the beginning and ending of a group. Whatever was found inside that group is replicated in the ReplaceWith box by using \1.

Finding [0-9,] means the find will find any digit or comma. Including an @ after that means it also finds more than one character instances of that pattern. It is the same as including {1,} instead of the @

If you want to also include periods you just include that in the pattern
Find = \[([0-9.,]@)\]
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 10-03-2022, 07:55 PM
BrianHoard BrianHoard is offline How to remove square brackets surrounding numbers with commas Windows 10 How to remove square brackets surrounding numbers with commas Office 2019
Advanced Beginner
How to remove square brackets surrounding numbers with commas
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Thank you sir! I learn so much from you all. We were trying to figure this out for hours today.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove square brackets surrounding numbers with commas Need Wild Card for Deleting Square Brackets with Differing Numbers inside CrossReach Word 6 11-26-2019 01:02 PM
What are the areas in square brackets [ ]? mummy Word 3 01-24-2018 05:15 AM
How to remove square brackets surrounding numbers with commas Bold numbers between square brackets jeffreybrown Word 4 02-13-2017 04:32 PM
How to remove square brackets surrounding numbers with commas Help with: Replace and reformat numbers inside square brackets johnbrowny Word 1 02-13-2016 06:45 PM
Gray square brackets waldux Word 8 09-25-2013 04:14 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft