Reputation: 7105
I need to extract text from an email body using a RegEx. I'm using Outlook 2010 with the MS VBScript 5.5 regex engine. The text i'm trying to extract is:
MessageID :1079247881
InstrumentID :DS5431460001
So far, I've come up with:
MessageID\s*\:(\d+)\n
InstrumentID\s+\:([a-z]{2}\d+)\n
Both are working now. Here are my tests:
Ultimately, I want to highlight a specific message and run my code. It will then extract those 2 items from that message and put them into a template for me to send. I'm doing ok on that part. I just need help with the RegExs.
Upvotes: 1
Views: 6644
Reputation: 28524
If you want to work with one regular expression, you could do something like this:
(?:Message|Instrument)ID\s+:(\w+)
I am unfamiliar with VB and its regexes implementation, but it seems you can do something like this (adapted and borrowed from here).
Function TestRegExp(myString As String)
'Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String
' Create a regular expression object.
Set objRegExp = New RegExp
'Set the pattern by using the Pattern property.
objRegExp.Pattern = "(?:message|instrument)ID\s+:(\w+)"
' Set Case Insensitivity.
objRegExp.IgnoreCase = True
'Set global applicability.
objRegExp.Global = True
'Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then
'Get the matches.
Set colMatches = objRegExp.Execute(myString) ' Execute search.
If (objRegExp.Test(myString) = True) Then
'Get the matches.
Set colMatches = objRegExp.Execute(myString) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
MsgBox objMatch.SubMatches(0)
Next
End If
End Function
Upvotes: 2