In this step, we start to show a lot of the features of Regular Expression Matching. Any set of characters enclosed by the square brackets ([]) define a character class. If I wanted to match only on a, b, c and d, I would use [abcd]. Similarly, if I wanted to match on zero through nine, I would use [0123456789]. Happily, these character classes support the hyphen to define a range. So, to match on any number 0 through 9, you can simply use [0-9], which I did. Now, the asterisk (*) is a very special character in that it says something about the character that came immediately before it. The period (.) and the question mark (?) work in the same way. The asterisk tells us to match on the previous character any number of times. So, we are essentially saying match on the URL, plus any number of any length.
In order to test to make sure this is true, I also added a link to an on-page bookmark in a URL. This is done with the pound (#) symbol. If something on the page had a link to it like this, <a name="12345">on-page link, and that bookmark was appended to the URL, our pattern match would stop short of it, because the pound sign is not part of the pattern.
Option Explicit
Dim myFakeEmail: myFakeEmail = "" & _
"I never received a response from anyone about my inquiry. Never spoke" & vbNewLine & _
"with Scala Re[. Don't remember any correspondence. I wanted to know" & vbNewLine & _
"the price of the software." & vbNewLine & _
"" & vbNewLine & _
"-----Original Message-----" & vbNewLine & _
"From: Scala Sales [mailto:Someone@Scala.com]" & vbNewLine & _
"Sent: Thursday, July 01, 2004 9:05 AM" & vbNewLine & _
"To: Prospect" & vbNewLine & _
"Subject: RE: Company, State, Country" & vbNewLine & _
"" & vbNewLine & _
"Prospect," & vbNewLine & _
"" & vbNewLine & _
"We have noticed the last update to your discussion with Scala Rep" & vbNewLine & _
"regarding ""Company, State, Country"" was 7 days ago. If you" & vbNewLine & _
"like, you can follow the included link..." & vbNewLine & _
"" & vbNewLine & _
"http://www.scala.com/message/?59483475213687#12345 " & vbNewLine & _
"" & vbNewLine & _
" ...and log in with the email address noone@nowhwere.com to" & vbNewLine & _
"let us know how your endeavors are proceeding. You can also reach" & vbNewLine & _
"Scala Rep by telephone at (555) 123-4567." & vbNewLine & _
"" & vbNewLine & _
"Regards," & vbNewLine & _
"" & vbNewLine & _
"Scala Sales" & vbNewLine & _
""
WScript.Echo(FindMessageID(myFakeEmail))
Function FindMessageID(messageBody)
Dim MyRegEx
Set MyRegEx = New RegExp
MyRegEx.IgnoreCase = True
MyRegEx.Global = True
MyRegEx.Pattern = "http://www.scala.com/message/\?[0-9]*"
Dim testMatch: testMatch = MyRegEx.Test(messageBody)
If testMatch = True Then
Dim myMatches: Set myMatches = MyRegEx.Execute(messageBody)
Dim firstMatch: Set firstMatch = myMatches(0)
FindMessageID = firstMatch.Value
Else
FindMessageID = ""
End If
Set firstMatch = Nothing
Set myMatches = Nothing
Set MyRegEx = Nothing
End Function
|
And the output confirms this is working...
|