Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #19  
Old 03-11-2019, 08:11 AM
totoMSOF totoMSOF is offline Regex over 700 matches in a long doc Windows 7 64bit Regex over 700 matches in a long doc Office 2010
Novice
Regex over 700 matches in a long doc
 
Join Date: Mar 2019
Posts: 11
totoMSOF is on a distinguished road
Default

I don't know who tagged this question SOLVED, because from my point of view, it was not. It was answered with another method than the one I was looking for. But I don't want people to give me fishes, I want to be taught to fish !
That's why I kept on digging the problem. Here it is a solution with the regex method.

After some tries, it appeared that the indexes thrown by the Execute method tend to shift by increasing multiple of 45 after some dozens of correct matches. For instance, the 22 first matches are correct, than the 18th next are shifted by 45 characters (before their exact place), than the 12 next are shifted by 90 characters (before...), and so on, to the 21 last which are shifted by 540 characters (= 45 x 12). Sometimes, for only one match, the shift is not exactly a multiple of 45, but one unit less...

I guess there are characters that put this shift from time to time, but I can't see where there are and I don't want to spend much time. If anyone has a clue about that, I will read carefully his idea .

So I changed the algorithm: instead of taking all the matches in one time and looping on the collection obtained, I change the regex in order to get the first match (Global property set to False), and I shift the range used to search that match by starting after the last match found (by the way, that allowed me to understand a little more how range works...). Of course, there is a great loss of time because the regex Execute searches to the end of the document over 700 times, but the code succeeds to perfectly place footnotes accross the whole document in a few dozains of seconds. Moreover, this duration includes a 2 printing 2 lines of debugging (by footnote created). So total time should decrease with some optimisation (may be the DoEvents is useless?). Showing the debug allows to see that the first regex Execute over almost all the document take a few 10th of second, whereas the last take only some 100th of seconds because the range is only a few pages...

Of course, the search method is right and certainly better in this case (I will study it later), but it doesn't answer the initial question about regex functioning.

Here is the code (with French names, sorry). Don't hesitate to comment about the clumsy things you see ...

Code:
Dim debut As Long
Dim fin As Long
Dim indexMatch As Long
Dim refCourte As String
Dim regTrouvees As MatchCollection
Dim rng As Range

Sub Remplacer_ref_2()

Set regex = New RegExp
With regex
    .IgnoreCase = False
    .MultiLine = True
    .Global = False
    .Pattern = " \(\[[A-Z][A-Z0-9-&*]+\](,[^\)]+|)\)"
End With

continuer = True
debut = 0

While continuer
    fin = ActiveDocument.Range.End
    Set rng = ActiveDocument.Range(Start:=debut, End:=fin)
    Debug.Print "DEBUT = " & debut & ", FIN = " & fin
    Set regTrouvees = regex.Execute(rng)
    If regTrouvees.Count > 0 And debut < fin Then
        indexMatch = regTrouvees(0).FirstIndex
        refTexte = regTrouvees(0).Value
        Debug.Print refTexte & " et index = " & debut + indexMatch
        Selection.SetRange Start:=indexMatch + debut, End:=indexMatch + debut
        Selection.Collapse Direction:=wdCollapseStart
        Selection.Footnotes.Add Range:=Selection.Range, Text:=refTexte
        debut = debut + indexMatch + 20
        Set regTrouvees = Nothing
    Else
        continuer = False
    End If
    DoEvents
Wend
MsgBox "FIN !"
End Sub
Reply With Quote
 

Tags
regex, replace

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex over 700 matches in a long doc Using VB Regex feature, I tried to replace 'the' and 'this' with 'that' but got screwed abdan Word VBA 3 01-18-2019 09:38 PM
How to compare 2 Excel sheets for 100+ matches? dylansmith Excel 5 05-22-2017 09:09 PM
Macro help regex subspace3 Word VBA 1 10-15-2014 09:53 AM
Regex over 700 matches in a long doc Convert RegEx to Word (Devanagari Font Find/Replace) gasyoun Word VBA 9 04-12-2013 04:15 PM
Regex in Word: Replaced strings are in disorder chgeiselmann Word 0 04-26-2009 11:33 AM

Other Forums: Access Forums

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


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