Home > Software design >  Regex - How to check if a string is matched the text but not the number?
Regex - How to check if a string is matched the text but not the number?

Time:02-08

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.

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.

CodePudding user response:

^(\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)

  •  Tags:  
  • Related