0

I'm getting over and over the following message error "Unexpected end of statement in the line 2" I can't see any thing wrong with it? What's I'm doing wrong ..

Sub Main()
  Dim regex As Regex = New Regex("\d+")
  Dim match As Match = regex.Match("Dot 77 Perls")
  If match.Success Then
      MessageBox.Show(match.Value)
  End If
End Sub
SierraOscar
  • 17,507
  • 6
  • 40
  • 68
rkachach
  • 16,517
  • 6
  • 42
  • 66

1 Answers1

6

You're mixing VB.NET syntax with VBScript, the code above should be written like so:

Dim regex
Set regex = CreateObject("VBScript.RegExp")

  regex.Pattern = "\d+"

  If regex.Test("Dot 77 Perls") Then
      WScript.Echo regex.Execute("Dot 77 Perls")(0)
  End If

More information on the VBScript Regular Expression engine can be found on this MSDN page

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • See edit above, should be `RegExp` not `Regex` - I missed that initially – SierraOscar Jun 09 '16 at 11:54
  • Are you actually writing this in VBA or in Visual Studio? – SierraOscar Jun 09 '16 at 11:55
  • Same error .. No, I'm using simple text editor. I don't have access to VS. I'm just trying to create some simple scripts for Outlook – rkachach Jun 09 '16 at 12:14
  • If you're using a text editor then this isn't VBA, it's VBScript - There's a huge difference which explains the errors. For future reference, VB.NET, VBScript and VBA are entirely separate languages. – SierraOscar Jun 09 '16 at 12:14
  • Yes, it's VBScript. Sorry for the confusion. I fixed it by using Set regex = New RegExp. Please could you update your answer so I will accept it. – rkachach Jun 09 '16 at 12:20
  • You also don't have to use `CreateObject` as `RegExp` is built-in to VBScript since version 5. Just use `Set regex = New RegExp`. – user692942 Jun 09 '16 at 13:06
  • @Lankymart I know - just left it there for compatibility, my previous revision included both early-binding and late-binding options (albeit for VBA, not VBScript) – SierraOscar Jun 09 '16 at 13:19