Skip to content

Instantly share code, notes, and snippets.

@huynle
Created June 21, 2017 17:23
Show Gist options
  • Select an option

  • Save huynle/7cf8dc64324b8efbebc8666c063b7295 to your computer and use it in GitHub Desktop.

Select an option

Save huynle/7cf8dc64324b8efbebc8666c063b7295 to your computer and use it in GitHub Desktop.
Function RegexExtract(ByVal text As String, _
ByVal extract_what As String, _
Optional separator As String = ", ") As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
Dim i As Long, j As Long
Dim result As String
RE.pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)
For i = 0 To allMatches.count - 1
For j = 0 To allMatches.Item(i).submatches.count - 1
result = result & (separator & allMatches.Item(i).submatches.Item(j))
Next
Next
If Len(result) <> 0 Then
result = Right$(result, Len(result) - Len(separator))
End If
RegexExtract = result
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment