Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Format of a string

What is the smallest amount of C# possible to Check that a String fits this format #-##### (1 number, a dash then 5 more numbers).

It seems to me that a regular expression could do this quick (again, I wish I knew regular expressions).

So, here is an example:

public bool VerifyBoxNumber (string boxNumber)
{
   // psudo code
   if (boxNumber.FormatMatch("#-#####")
      return true;
   return false;
}

If you know real code that will make the above comparison work, please add an answer.

like image 669
Vaccano Avatar asked Dec 06 '10 21:12

Vaccano


2 Answers

private static readonly Regex boxNumberRegex = new Regex(@"^\d-\d{5}$");

public static bool VerifyBoxNumber (string boxNumber)
{
   return boxNumberRegex.IsMatch(boxNumber);
}
like image 65
cdhowie Avatar answered Sep 26 '22 09:09

cdhowie


return Regex.IsMatch(boxNumber, @"^\d-\d{5}$");
like image 32
LukeH Avatar answered Sep 26 '22 09:09

LukeH