View Single Post
 
Old 10-03-2022, 03:02 PM
BrianHoard BrianHoard is offline Windows 10 Office 2019
Advanced Beginner
 
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