question

LeePolikoff-1397 avatar image
0 Votes"
LeePolikoff-1397 asked LeePolikoff-1397 edited

Regular Expression in Word VBA Is Not Returning Correct Value

I have regular expression that I tested online to make sure it was correct. When I use it in MS Word, it is returning more than just the value I want it too.

Here is the Regular Expression ([nN][0-9].*-[tT].\S*[0-9])

Here is the code I'm using in Word VBA:

 Dim regEx As VBScript_RegExp_55.RegExp
 Set regEx = New VBScript_RegExp_55.RegExp
 Dim Matches As VBScript_RegExp_55.matchCollection
 Dim Match As VBScript_RegExp_55.Match
    
 With regEx
     .IgnoreCase = False
     .MultiLine = True
     .Global = False    ' Only look for 1 match; False is actually the default.
     .pattern = "([nN][0-9].*-[tT].\S*[0-9])"  ' Word separates lines with CR (\r)
 End With
 Set Matches = regEx.Execute(ActiveDocument.Content.Text)
 For Each Match In Matches
     MsgBox (Match.value)
 Next Match

Here is an example of the text in my word document:

Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodologically knocking people's hats off - then, I account it high time to get to sea as soon as I can. N1.2.3-T1-Test-4.5-S1
This is my substitute for pistol and ball... I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me:

Fish
Whales
Sharks
Ships
N1.2.3-T1-Test-4.5-S1
N1.2.3-T1-Test-4.5-S1
N1.2.3-T1-Test-4.5-S1
N1.2.3-T1-Test-4.5-S1

If I run the code it returns the following in the msgbox:
82933-popup.png

You can see that it returns from the first match all the way to the last match. It should return 5 distinct matches. If I didn't have the 4 line items at the end it would work fine. My document will have multiple instances that need to be matched and I need to find each one.


office-vba-dev
popup.png (6.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

Try this pattern:

 .Pattern = "([nN][0-9].*?-[tT].\S*[0-9])"

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeePolikoff-1397 avatar image
0 Votes"
LeePolikoff-1397 answered LeePolikoff-1397 edited

This suggestion works. Thank You :)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.