-
Viorel 112.5K Reputation points
2021-03-30T20:26:03.94+00:00 Try this pattern:
.Pattern = "([nN][0-9].*?-[tT].\S*[0-9])"
This suggestion works. Thank You :)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
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.
Try this pattern:
.Pattern = "([nN][0-9].*?-[tT].\S*[0-9])"
This suggestion works. Thank You :)