I would tackle this problem in two parts.
I would split up the text by spaces like you're doing
I would then run through that list of words and remove any query terms that are non-alphanumeric.
The following is an example of that:
' ... Your Other Code ...
' A function to determine if a string is AlphaNumeric
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
Dim IsAlpha As Boolean = False
If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
IsAlpha = True
IsAlpha = False
' A function to get the words from the textbox
Private Function GetWords() As String()
' Get a raw list of all words separated by spaces
Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+")
' ArrayList to place all words into:
Dim alWords As New ArrayList()
' Loop all words and check them:
For Each word As String In splitQuery
' Word is alphanumeric
' Add it to the list of alphanumeric words
' Convert the ArrayList of words to a primitive array of strings
Dim words As String() = CType(alWords.ToArray(GetType(String)), String())
' Return the list of filtered words
This code does the following:
- splits up the textbox's text
- declares an
ArrayList for the filtered query terms/words
- loops through all the words in the split up array of terms/words
- it then checks if the term is alphanumeric
- If the term is alphanumeric, it is added to the
ArrayList. If it's not alphanumeric, the term is disregarded.
- Finally, it casts the terms/words in the
ArrayList back to a normal String array and returns.
Because this solution uses an
ArrayList, it requires
System.Collections as an import.