I'm trying to implement a search function in a program I am writing. At the moment, I have a list of strings which I am searching through.
What I'd like to do is check whether all words that have been typed into a search box appear in the search results. However, the catch is that they can appear in any order. So for example, if the string is:
The quick brown fox jumps over the lazy dog
and I were to type:
dog fox brown
It would return a positive match.
My first thought was to .split
the words in the search box into an array. Then, iterate through the list of strings, and check to see if the string contained every word in the array, else, remove it from the list. Once all the strings have been iterated through, display the results.
However, I can imagine that there would be a significant amount of overhead, especially because I'd like the filtering to be done real-time, ie. attached to the TextChanged event of the textbox.
Is there a more efficient way of doing this filtering? I just wanted to make sure that there wasn't a more efficient way of doing this before I started coding it.
I propose Regex.Matches().Count
in a Linq .Select().Sum()
Given the provided input string:
The quick brown fox jumps over the lazy dog
Counts the number of patterns that are a match in that string and the number of patterns (words).
Given the patterns contained in the string: (and assuming that the separator character is chr(32)
)
dog fox brown
Imports System.Text.RegularExpressions
Dim words As Integer
Dim NumberOfMatches As Integer = GetMatchingWords(
"The quick brown fox jumps over the lazy dog",
"dog fox brown",
words)
Public Function GetMatchingWords(TextToSearch As String, WordsToMatch As String, ByRef words As Integer) As Integer
words = Regex.Matches(WordsToMatch, ChrW(32)).Count + 1
Return WordsToMatch.Split(ChrW(32)).Select(
Function(word) Regex.Matches(TextToSearch, word).Cast(Of Match)().Count()).Sum()
End Function
This Function returns:
words = 3
NumberOfMatches = 3<br>