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

teknovationAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

could you send an example that does not work

Regards
Dan CraciunIT ConsultantCommented:
Easy, but probably wrong:
string pattern = @"(-[A-Za-z.]*\d+[A-Za-z.]*(-[A-Za-z.]*\d+[A-Za-z.]*)*)";

Unless we can see the strings you're matching against and the desired result, I don't think we can help more.

HTH,
Dan
teknovationAuthor Commented:
Sorry - some items to search for are in this pattern that we cannot get it to appear

100-200-300-1.5
100-200-300-2S
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Rgonzo1971Commented:
Hi,

pls try

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

Open in new window

Regards
Dan CraciunIT ConsultantCommented:
string pattern = @"(-[A-Za-z]*\d+[A-Za-z]*(-[A-Za-z]*\d+\.?\d*[A-Za-z]*)*)";
teknovationAuthor Commented:
Rgonzo1971 - it didn't work
Dan Craciun - it didnt' work either
teknovationAuthor Commented:
^bump
Dan CraciunIT ConsultantCommented:
This is a much shortened version of the initial string. In your code I see no use of the numbered capturing groups, so a non-capturing group is more efficient.

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

Open in new window


PS: did not work what?
What result did you receive?
Dan CraciunIT ConsultantCommented:
Or even shorter, although possibly including characters from other languages:
string pattern = @"(?:-\p{L}*\d+\.?\d*\p{L}*)+";

Open in new window

teknovationAuthor Commented:
its still not working is the one item I'm trying to search for and it's not working

100-201-103-0.2
Dan CraciunIT ConsultantCommented:
I don't think the regex expression is the issue. Can you please post the constructor for the KeywordSearchCriteria object?
teknovationAuthor Commented:
Could it have something to do with :
string replacement = Regex.Replace(searchText, pattern, "$0*");

/// <summary>
/// Summary description for  Search Page
/// </summary>
public class Search
{
private KeywordSearchCriteria keywordSearchCriteria;
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
You should really be posting examples of inputs and their corresponding, expected outputs. Otherwise, we're all just playing a game of "guess and check," which isn't a very fun game.
teknovationAuthor Commented:
Ok figured out the issues since i hard coded it and it worked. The way it's passed to the replacement string is where it's incorrect?

try
        {
                //Try strong Regex
            string replacement = Regex.Replace(searchText, pattern, "$0*");
                  keywordSearchCriteria = new KeywordSearchCriteria { QueryText = "100-201-103-0.2" };
Dan CraciunIT ConsultantCommented:
With the current regex, what is passed is this:
QueryText = "-201-103-0.2*"
teknovationAuthor Commented:
ok thats not right...how do i fix it?

How do I display the variable for the .cs file for web? ive been updating the .cs file and saving it and it refreshes the website
Dan CraciunIT ConsultantCommented:
If you really want the full string as a search pattern, you can use the following code. But I don't think that's the intended usage:

 
string pattern = @"(?:\p{L}*\d+\.?\d*\p{L}*-?)+";
        try
        {
            //Try strong Regex
            string replacement = Regex.Replace(searchText, pattern, "$0");
            keywordSearchCriteria = new KeywordSearchCriteria { QueryText = replacement };

Open in new window

teknovationAuthor Commented:
dan what do you mean?
Dan CraciunIT ConsultantCommented:
I mean it will work for this specific case.

But it was designed for partial searches (that's what the * in "$0*" was for), so you should check if it still works the same for the strings without periods in them.
teknovationAuthor Commented:
Hi Dan,

Thank you for the response and yes, you are correct. We have items in our database that have different patterns in the likes of combining both alpha-numeric values. A typical Item Number would look like this:

100-ABC-200-300
123-456-789
K8012
A123
ABC1234
ABC-21
ABC-100
A123-12
AB-123
AB-012-1023
ABCD-12
ABCD-123
ABCD-1234
A123-123
AbcDefg-1234
123-456-789-1

The regexp pattern code seems to capture all of these except anything that ends in a letter OR if there's a decimal. Two examples where this is not showing up in the search:

123-456-789-0.1
123-456-789A

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

Open in new window

Any thoughts on how to modify the pattern so that it works?
teknovationAuthor Commented:
ok i manage to get output from the code.

(-[A-Za-z]*\d+[A-Za-z]*(-[A-Za-z]*\d+[A-Za-z]*)*)

So with this expression, the output is:

123-456-789-1*.1

The value we need to search for is 123-456-789-1.1
Dan CraciunIT ConsultantCommented:
Most of the solutions proposed will give you 123-456-789-1.1. Which will be transformed into "123-456-789-1.1*", which apparently does not work with your search engine.

We already gave you correct expressions so the strings with dots in them are accepted.
Why the search does not work when you add a "*"? Not enough info to answer that question.
Either contact the original programmer, post all the code here or get a new programmer to fix the bug.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
teknovationAuthor Commented:
HI Dan, after spending some more time at this, i figured out. It was just adding *\d+\. to the end of it. Thanks again for your help though.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.