Submatches in RegExp from LotusScript

As previously described you can use the Visual Basic Script RegExp component for regular expression matching on Windows. The component supports backreferencing and submatches which makes it easy to extract the results from pattern matching from the text. No more Instr’ing and Mid’ing to extract the information.

The below example shows you how to extract the id from a URL with an id-parameter. The output from the example will be two messageboxes stating:

  • We matched: http://www.example.com?id=123
  • Submatch 1: 1234

Sub Initialize
   'declarations
   Dim regexp As Variant
   Dim rc As Integer
   Dim text As String
   Dim submatch_count As Integer

   'set text
   text = |This is a random link http://www.example.com?id=1234 where we want to extract the id.|

   'create object
   Set regexp = CreateObject("VBScript.RegExp")

   'make pattern matching case insensitive
   regexp.IgnoreCase = True

   'set pattern
   regexp.Pattern = |http://.*?id=([d]*)|

   'test pattern
   Dim matches As Variant
   Set matches = regexp.Execute(text)
   Forall m In matches
      Msgbox "We matched: " + m.Value
      submatch_count = 1
      Forall submatch In m.Submatches
         Msgbox "Submatch " & submatch_count & ": " & submatch
         submatch_count = submatch_count + 1
      End Forall
   End Forall

End Sub