Log in

View Full Version : Wildcard search takes a long time to run



johndavidson
06-28-2013, 01:08 AM
I have VBA code running under Word 2003 that includes the following wildcard search:

.Text = "(*^11){2,}"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True

It takes ages to run. Even if I do the search as a regular Word dialog box search on a 10,000 character document, pasting in the find and research fields, it takes 7 secs on a 10,000 word doc on an i7 64-bit computer. It is the searching that takes the time. It even takes ages to find nothing at all!

Does anyone know why it is taking so long and/or can think of an alternative way of presenting the search?

Thanks

Doug Robbins
06-29-2013, 09:14 PM
Try using (*^11)^11{1,} instead.

johndavidson
06-29-2013, 09:58 PM
Thanks Doug, but the idea is to replace two or more identical strings that are each terminated by a new line (^11) with just one copy of the string.

Based on your thinking, I tried (*^11)(*^11){1,} - which works under simple circumstances, but when there are instances of the same string in different locations, some weird and wonderful (and unwanted) replacements take place, for which I have yet to figure out the logic!

Also, when you exit the F-R dialog box, Word sometimes crashes!! I haven't figured out the circumstances yet that cause the crash because it is intermittent, nor have I tried it programmatically yet.

I'm attaching a word document (aatest.doc) in case you feel like tinkering. I'm working in Word 2003, btw.

Thanks

John

johndavidson
06-30-2013, 06:58 AM
I realized what the problem is. (*^11) will find all text that precedes a ^11, not just text on a single line. To find matches between two identical strings, for each line ending in ^11, Word will have to perform many searches for each possible (*^11). This will take ages and ages, which it does.

I've accomplished the desired deletion of duplicates lines by setting the text that may contain duplicates to a different Word style, and have added that style to the wildcard search/replace.