Reputation: 171
I use VBA code (using Microsoft VBScript Regular Expressions 5.5 library) to replace plain text ID in mails from our bug tracking system to hyperlink.
The message can contain stings like "Bug 1234567" or "Issue 1234567".
I use the following code to match first option:
Set re = CreateObject("vbscript.regexp")
re.Pattern = "Bug [0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
For Each match In re.Execute(body)
MsgBox match.Value
Next
I would like to match both options at once.
The following patterns don't work. Only one option still matched (replaced).
re.Pattern = "(Bug|Issue) [0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
re.Pattern = "(Bug [0-9][0-9][0-9][0-9][0-9][0-9][0-9])|(Issue [0-9][0-9][0-9][0-9][0-9][0-9][0-9])"
Upvotes: 0
Views: 641
Reputation: 999
Your pattern is fine, but it can be simplified by explicitly stating that it should contain [0-9]
7 times, i.e.:
(Bug|Issue) [0-9]{7}
What you're missing is the Global
property, which according to this site specifies that the regular expression should match all occurrences - not just the first. So your entire code becomes:
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "(Bug|Issue) [0-9]{7}"
For Each match In re.Execute(body)
MsgBox match.Value
Next
Upvotes: 1