0

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

1
  • Try this Regular expression : (best\s\d*)|(\d*\sbest)|(\d) Commented Jan 17, 2014 at 16:08

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

From OP: "Entire string can only be alphanumeric (including spaces)". I think you should change the . to a character class.
@Vache I wonder though why OP is using .* themself. Plus, the strings that are not supposed to match don't seem to have to do anything about being alphanumeric including spaces, but really about the placement of best and the number. Otherwise, I guess we just have to wait for OP to clarify...
@Jerry : His trying to get best or 5 in the string pattern but your regex capture whole string. If i am wrong let me know ???
@AnuragJain My regex doesn't capture the whole string. It matches the whole string. The second regex I suggested captures only best 5 and 5 best...
0

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"

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.