Regex - wild card search results help!

teknovation
teknovation used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Hi,

could you send an example that does not work

Regards

Commented:
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

Author

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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Top Expert 2016

Commented:
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

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

Author

Commented:
Rgonzo1971 - it didn't work
Dan Craciun - it didnt' work either

Author

Commented:
^bump

Commented:
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?

Commented:
Or even shorter, although possibly including characters from other languages:
string pattern = @"(?:-\p{L}*\d+\.?\d*\p{L}*)+";

Open in new window

Author

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

Commented:
I don't think the regex expression is the issue. Can you please post the constructor for the KeywordSearchCriteria object?

Author

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;
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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.

Author

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

Commented:
With the current regex, what is passed is this:
QueryText = "-201-103-0.2*"

Author

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

Commented:
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

Author

Commented:
dan what do you mean?

Commented:
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.

Author

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?

Author

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
Commented:
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.

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial