#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
Wow Macropod! You are amazing! This is working so far, so good. We're running it on our docs now.
|
#4
|
|||
|
|||
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] |
#5
|
||||
|
||||
() 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 |
#6
|
|||
|
|||
Thank you sir! I learn so much from you all. We were trying to figure this out for hours today.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Bold numbers between square brackets | jeffreybrown | Word | 4 | 02-13-2017 04:32 PM |
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 |