Code: protected ArrayList parseSearchTerms(string searchTerms) { //parse the query string ArrayList exactPhrases = new ArrayList();
//look for pairs of quotes as 'exact matches' string[] stdoubles = searchTerms.Split("\"".ToCharArray()); if (stdoubles.Length > 1) { for (int i = 1; i < stdoubles.Length - 1; i = i + 2) { if (stdoubles[i].Length == 0) { continue; } exactPhrases.Add(stdoubles[i]); } } //now remove them from the original search phrase foreach (object item in exactPhrases) { searchTerms = Regex.Replace(searchTerms, "\"" + item.ToString() + "\"", string.Empty); } //do the same for single quotes string[] stsingles = searchTerms.Split("'".ToCharArray()); if (stsingles.Length > 1) { for (int i = 1; i < stsingles.Length - 1; i = i + 2) { if (stsingles[i].Length == 0) { continue; } exactPhrases.Add(stsingles[i]); } } //now remove them from the original search phrase foreach (object item in exactPhrases) { searchTerms = Regex.Replace(searchTerms, "'" + item.ToString() + "'", string.Empty); }
//take the remaining terms and break them into one-word "phrases" string[] st = searchTerms.Split(" ".ToCharArray()); if (st.Length > 0) { for (int i = 0; i < st.Length; i++) { if (st[i].Length == 0 || !Regex.IsMatch(st[i], "[a-zA-Z0-9]")) { continue; } exactPhrases.Add(st[i]); } } return exactPhrases; }
|