troubleshooting Question

Regex - wild card search results help!

Avatar of teknovation
teknovation asked on
Regular ExpressionsC#
23 Comments1 Solution117 ViewsLast Modified:
Does anyone know how I can tweak this string pattern to accept decimals? Right now we are able to search and find things but once there's a decimal in the string, the search cannot find it. It looks to be using Regex.

 
string pattern = @"(-[A-Za-z]*\d+[A-Za-z]*(-[A-Za-z]*\d+[A-Za-z]*)*)";


private SearchResponseData GetproductSearchResult(string searchText, string category = "")
    {
        //WILDCARD results if search term contains a Catalog Number
        string pattern = @"(-[A-Za-z]*\d+[A-Za-z]*(-[A-Za-z]*\d+[A-Za-z]*)*)";
        try
        {
            //Try strong Regex
            string replacement = Regex.Replace(searchText, pattern, "$0*");
            keywordSearchCriteria = new KeywordSearchCriteria { QueryText = replacement };
        }
        catch (Exception ex)
        {
            //If syntax error, try weaker Regex
            if (Regex.IsMatch(searchText, "-(\\d)*"))
            {
                keywordSearchCriteria = new KeywordSearchCriteria { QueryText = searchText + "*" };
            }
            //Use search term as-is
            else
            {
                keywordSearchCriteria = new KeywordSearchCriteria { QueryText = searchText };
            }
        }
}
ASKER CERTIFIED SOLUTION
Dan Craciun
IT Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros