Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex – How to check if a string is matched the text but not the number?

I want to check a string is matched so I will do some business logics with it.

The pattern is very simple.

If it matches 100% text but different number so it’s a match.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example:

Pattern: "xxx is a large number" which xxx must be a integer number (not null, not empty, not text, not double number)

  1. "123 is a large number" => match
  2. "444444 is a large number" => match
  3. "is a large number" => not match
  4. "123 is not a large number" => not match
  5. "Test is a large number" => not match

My code:

var pattern = "^[0-9]+$ is a large number";
var testText = "123 is a large number";
var match = Regex.Match(testText, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
    //do some business logics
}

This is the Regex I try but doesn’t work:

"^[0-9]+$ is a large number"

Thank you.

>Solution :

^(\d)+ is a large number$

  • ^ for the start of the string

  • \d+ for a digit, 1 or more times

  • is a large number$ for the rest of the string (and $ to signify the end)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading