Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
Supporting the Microsoft .NET Developer Community.

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:11:49 PM CT Feb 15, 2007
Posted:01:10 AM CT Jan 27, 2007

Extract Keywords from a Search String in C#

Author: Steve Lautenschlager

Has DownloadsSnippetBeginnerRegular ExpressionsText and StringsC#.NET

 Downloads

Extract Keywords From Search String.zip - 9.5 KB - Windows Forms Solution (.NET Version 1.1) C#

 Summary

When a user enters a string into a textbox to initiate a search, the developer has to parse that string and convert it to a query of some kind. This little snippet uses a very simple regular expression to extract the keywords from the search string as an array of strings. From this array you can then create your search query.
public static string[] GetSearchWords(string text)
{
	string pattern = @"\S+";
	Regex re = new Regex(pattern);

	MatchCollection matches = re.Matches(text);
	string[] words = new string[matches.Count];
	for (int i=0; i<matches.Count; i++)
	{
		words[i] = matches[i].Value;
	}
	return words;
}
Dont' forget the using directive:

using System.Text.RegularExpressions

 Screenshot of sample application in the download

Add New Comment
Extract Keywords from a Search String in C#
troy23 Sep 08, 22:33Reply 
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve is the founder and creator of Cambia Research. Developing and maintaining the site combines his passions for technology, writing and education.
Steve holds a Ph.D. in particle physics from Duke University, has worked at CERN, the European center for particle physics (where the web was born) and in Microsoft's web division with microsoft.com, msnbc.com and other web properties. Steve is a web consultant specializing in Microsoft.NET technologies. Read more here.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com