Avatar of teknovation
teknovation

asked on 

Regex - wild card search results help!

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 };
            }
        }
}

Open in new window

Regular ExpressionsC#

Avatar of undefined
Last Comment
teknovation

8/22/2022 - Mon