kashif4u
kashif4u

Reputation: 175

Regex Match Alphanumeric values with specific conditions

I need to match a string under the following conditions using Regex in C#:

Entire string can only be alphanumeric (including spaces). Example string should only match are: ( numerical values can change)

Example1 String: best 5 products

Example2 String: 5 best products

Example3 String: products 5 best

I am looking to get get "5 best" or "best 5" but the following strings are also matching:

Example1 String: best anything 5 products

Example2 String: 5 anything best products

Example3 String: products 5 anything best

I am using:

string utter11 = Console.ReadLine();

string pattern11 = "^(?=.*best)(?=.*[0-9]).*$";

bool match = Regex.IsMatch(utter11, pattern11, RegexOptions.IgnoreCase);
Console.WriteLine(match);

Any suggestions are welcome. Thanks

Upvotes: 0

Views: 274

Answers (3)

mrzli
mrzli

Reputation: 17359

How about this (complete example):

class Program
{
    static void Main(string[] args)
    {
        List<string> validStrings = new List<string>
        {
            "best 5 products",
            "5 best products",
            "products 5 best",
            "best anything 5 products",
            "5 anything best products",
            "products 5 anything best",
        };

        List<string> invalidStrings = new List<string>
        {
            "best 5 products.",
            "5 best product;s",
            "produc.ts 5 best",
            "best anything 5 product/s",
            "5 anything best produc-ts",
            "products 5 anything be_st",
        };

        string pattern1 = @"^(([A-Za-z0-9\s]+\s+)|\s*)[0-9]\s+([A-Za-z0-9\s]+\s+)?best((\s+[A-Za-z0-9\s]+)|\s*)$";
        string pattern2 = @"^(([A-Za-z0-9\s]+\s+)|\s*)best\s+([A-Za-z0-9\s]+\s+)?[0-9]((\s+[A-Za-z0-9\s]+)|\s*)$";
        string pattern = string.Format("{0}|{1}", pattern1, pattern2);

        foreach (var str in validStrings)
        {
            bool match = Regex.IsMatch(str, pattern);
            Console.WriteLine(match);
        }

        Console.WriteLine();

        foreach (var str in invalidStrings)
        {
            bool match = Regex.IsMatch(str, pattern);
            Console.WriteLine(match);
        }

        Console.Read();
    }
}

If you have more examples of what string the pattern should and shouldn't match, I'll refine the expression if necessary.

Upvotes: 0

Jerry
Jerry

Reputation: 71538

You might try this which I made as close as possible to your regex:

^(?=.*(?:best [0-9]|[0-9] best)).*$

regex101 demo

If you want to get capture groups, just make a minor change:

^(?=.*(best [0-9]|[0-9] best)).*$

regex101 demo

It's basically looking for best [0-9] or [0-9] best, which I understood is what you're looking for.

Upvotes: 1

Patrick Quirk
Patrick Quirk

Reputation: 23747

Try (?:(?<=best)\s+([0-9]))|(?:([0-9])\s+(?=best))

Expects either a prefix of "best" and then whitespace and a number, or a number and whitespace and then a suffix of "best"

Upvotes: 0

Related Questions