PDA

View Full Version : [SOLVED:] wildcard pattern matching fun



erwarren
08-12-2015, 02:40 PM
Long time reader, first time poster here. I can't seem to figure this one out, so any help would be greatly appreciated.

Background:
I have a macro that produces a list of all the paragraph styles in use in this format:



Normal -- p. 1
Heading 1 -- p. 12
No Spacing -- p. 144


Each style in use is only listed once, and the page numbers, of course, will vary based on the document.

If the only styles in use are "Endnote Text" and/or "Footnote Text" something went wrong so I need to check if the list consists of ONLY combinations of those. My plan is to paste the string that contains the list in a new document so I can use Selection.Find and wildcards to search the text, and then check if the length of the Selection.Text is the same as the length of the whole list. Ideally I'd have only 1 (possibly 2) patterns that could find all combinations of those two styles.

Actual Problem:
I'm starting by trying to find a wildcard search that will find either "Endnote Text -- p. 000" or "Footnote Text -- p. 000" (where 000 is an unknown page number), and then I figure I can double it up to search for both. I've come up with this, entered as an image because my post got rejected with this string in it:

14139

But it only finds "Footnote Text -- p. 000", not "Endnote Text -- p. 000". I think the issue is the {3,4} part, which I took to mean "three or four instances of the previous expression". If I change it to just {3} I match "Endnote Text -- p. 000" as expected as well as the "ootnote Text -- p. 000" of the other style, but once I add the range it stops matching "Endnote Text -- p. 000".

That's it in a nutshell, but here's the code I've written for the whole sub so far.



Sub test()
' This will ultimately be a boolean function and strSearchPattern
' and strSearchText will be passed to it when called

Dim strSearchPattern As String
Dim strSearchText As String

' Here would be the same string in the image above
strSearchText = "Endnote Text -- p. 100"

Documents.Add Visible:=False

Selection.InsertAfter (strSearchText)

With Selection.Find
.ClearFormatting
.Text = strSearchPattern
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWholeWord = False
.MatchCase = True
.MatchWildcards = True
.MatchSoundsLike = False
.Execute
End With

If Selection.Find.Found = True Then
If Len(Selection.Text) = Len(strSearchText) Then
Debug.Print "True"
Else
Debug.Print "False"
End If
End If

ActiveDocument.Close wdDoNotSaveChanges


End Sub


Also FWIW, I'm using Word 2013 on Windows 8 but this needs to work for all versions 2007-2013, including Mac 2011.

gmayor
08-12-2015, 11:13 PM
You are going to have to break up the search pattern as the letters are repeated in the words and this creates confusion. The following should work based on your example

[EF]{1}[dnot]{4}[eot]{2,} Text -- p. [0-9 ]{1,}^13

http://www.gmayor.com/replace_using_wildcards.htm

erwarren
08-13-2015, 06:26 AM
Thanks so much, that's just what I was looking for! It matches a single line of Endnote Text -- p. 000" or "Footnote Text -- p. 000" perfectly. However if I double up the pattern to look for instances when both lines are present, I get an error saying "The Find What text contains a Pattern Match expression which is too complex." Does this mean I am out of luck on that part, or is there a way to append an {1,2} to apply to an entire expression like the one you posted?

Thanks so much for your help!

gmayor
08-13-2015, 11:29 PM
I don't understand. Why are you doubling up? The pattern will find either text assuming they are in separate paragraphs, which is what I understood was the case? As for the {1,2} part then no that is not possible.

erwarren
08-17-2015, 02:23 PM
Yes it finds either case, but I was hoping to differentiate between "Endnote Text" or "Footnote Text" alone, or the case where they both exist. I don't know if I can do that because of the pattern matching complexity, but your solution works for finding either of the single cases which is a huge help.