Compare Strings by non-value format

7

I have a format of String , for example:

  • xxxx-xxxx-xxx

However, instead of x you can enter several letters or numbers, for example:

  • A216-0450-013
  • X2LP-1018-589
  • Y585-0000-047

What I need to do is compare to see if the format is the same, but the value does not matter.

    
asked by anonymous 17.04.2017 / 22:28

1 answer

8

Using Regex.IsMatch() you can check if a string is within the expected pattern, below is an implementation.

Do not forget to include the namespace System.Text.RegularExpressions

  

using System.Text.RegularExpressions;

string pattern = @"^.{4}-.{4}-.{3}$";
string[] input = { "A216-0450-013" , "X2LP-1018-589",   "Y585-0000-047" , "585-0000-047" };

foreach(var item in input)
{
    if(Regex.IsMatch(item,pattern))
        Console.WriteLine("{0} is Match",item);
    else
        Console.WriteLine("{0} does not Match",item);
}

See working at .Net Fiddle

    
17.04.2017 / 22:52