This is the class that can be used to look up a keyword from a string. This class is inherited from Regex class.
Base Class
public class ProductLookupRegex : Regex
{
public ProductLookupRegex()
{
//
// TODO: Add constructor logic here
//
}
///
/// Initializes a wildcard with the given search pattern.
///
///
The wildcard pattern to match.
public ProductLookupRegex(string pattern) : base(WildcardToRegex(pattern))
{
}
///
/// Initializes a wildcard with the given search pattern and options.
///
///
The wildcard pattern to match.
///
A combination of one or more
/// .
public ProductLookupRegex(string pattern, RegexOptions options): base(WildcardToRegex(pattern), options)
{
}
///
/// Converts a wildcard to a regex.
///
///
The wildcard pattern to convert.
/// A regex equivalent of the given wildcard.
public static string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern).
Replace("\\*", ".*").
Replace("\\?", ".") + "$";
}
}
How to use it
ProductLookupRegex productlookup =
new ProductLookupRegex(productName,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
foreach (DictionaryEntry de in prodList)
{
if (productlookup.IsMatch(de.Key.ToString().Trim()))
{
productLink = LinkGenerator(de.Value.ToString());
productName = de.Key.ToString();
break; // (sn)
}
}