Scala - Digital Signage Software [Logo]
Languages: EnglishGermanPolishJapaneseNorwegianFrench
  Markets Products Partners Services Support About Us  
Scala Support Scala Celebrates 20 Years of Innovation and Leadership

Jul 01, 2004: Using the Right() and Len() functions to chop off the URL

We're almost done. I've only just scratched the surface of Regular Expression Matching, hardly even introducing you to the complex rules. But it's time to cheat a little bit. As you may have surmised, regular expression matching is about consuming characters in your match. You write a pattern that grabs and includes characters. Consequently, even though we are just interested in the number from the URL, we had to grab the entire URL, because it is the URL that tells us where the number is found in the document. Do you detect a little something of a catch-22 in regular expression matching? It is true!

But any faculty you could imagine asking for is usually available in RegEx, although support varies between implementations. For example, to do the entire task with just one match, I would use the "look behind" feature. In other words, the part of my pattern that included the URL would not consume the text. I would be looking for a number that matches the pattern, which has the URL before it. This is different from the URL followed by the number. As you will learn with RegEX, it's all in how you state the question. There is much order sensitivity.

So, for the cheating I talked about. Since the URL is the same every time, and it is of a fixed length, it is easy to chop it off of the beginning of the string before we return the value. We will use VBScript's built-in string functions to do this. It could be done with a second RegEX match. But I think that's overkill at this point. The Right() and Len() functions fit the need very nicely...

Option Explicit

Dim myFakeEmail: myFakeEmail = "" & _
"I never received a response from anyone about my inquiry. Never spoke" & vbNewLine & _
"with Scala Rep. 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, chopMe
     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)
          chopMe = firstMatch.Value
          chopMe = Right(chopMe, Len(chopMe) - Len("http://www.scala.com/message/?"))
          FindMessageID = chopMe

     Else
          FindMessageID = ""
     End If
     Set firstMatch = Nothing
     Set myMatches = Nothing
     Set MyRegEx = Nothing
End Function


And the output of our program demonstrates that we are done...



Additional Information
Request DVD - Concepts and demo
Contact Scala Sales
Search