Welcome to FreeSoftwareServers Confluence Wiki

https://stackoverflow.com/a/69683944/5079799

Sub TestRegEx()
 Dim TextStr As String, PatternStr As String
 TextStr = "StartStr Ab12345678 EndStr"
 PatternStr = "(([a-z]{2})([0-9]{8}))"
 Debug.Print ExtractSubStrWRegEx(PatternStr, TextStr)
End Sub
Function ExtractSubStrWRegEx(ByVal PatternStr As String,ByVal TextStr As String) As String

 Dim RE As Object
 Set RE = CreateObject("vbscript.regexp")

 RE.Pattern = PatternStr
 RE.Global = True
 RE.IgnoreCase = True

 Dim allMatches As Object
 Set allMatches = RE.Execute(TextStr)

 If allMatches.Count <> 0 Then
  ExtractSubStrWRegEx = allMatches.Item(0).submatches.Item(0)
 Else
  ExtractSubStrWRegEx = ""
 End If

End Function
  • No labels