Skip to content

Instantly share code, notes, and snippets.

@ssta604
Created March 14, 2019 06:33
Show Gist options
  • Save ssta604/7a1e895af212b12f08cbf760acb65946 to your computer and use it in GitHub Desktop.
Save ssta604/7a1e895af212b12f08cbf760acb65946 to your computer and use it in GitHub Desktop.
Imports System.Text.RegularExpressions
Public Class RegExHelper
Public Shared Function GetRegExMatches(ByVal inputText As String, ByVal searchText As String) As MatchCollection
If String.IsNullOrWhiteSpace(searchText) Then
Return Nothing
End If
Dim lSafeSearchText As String
'Break apart the search into string array split by one or more space
Dim lWords() As String = Regex.Split(searchText, "\s+")
'Join the words back togeter to form a new RegEx with the escaped words separate by the OR operator (|)
Dim sb As New System.Text.StringBuilder
For Each lWord As String In lWords
'Escape the word in case there are any RegEx metacharacters in the string
sb.Append(Regex.Escape(lWord))
'Build OR
sb.Append("|")
Next lWord
If sb.Length > 0 Then
'Convert to string stripping the extra |
lSafeSearchText = sb.ToString(0, sb.Length - 1)
Return Regex.Matches(inputText, lSafeSearchText, RegexOptions.IgnoreCase)
Else
Return Nothing
End If
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment