Match phone numbers : Regex Phone number « Regular Expression « C# / CSharp Tutorial






using System;
using System.Text.RegularExpressions;
class RegExp 
{
  public static void Main()
  {
    String[] phone_nos = {"111-123-4567","1111-1234-4567"};
    String phone_regexp = "[0-9]{3}-[0-9]{3}-[0-9]{4}";
    foreach (String phone in phone_nos)
    {
      Match m = Regex.Match(phone,phone_regexp);
      if (m.Success)
      {
        Console.WriteLine(phone+" is Valid");
      }
      else
      {
        Console.WriteLine(phone+" is Not Valid");
      }
    }
  }
}








17.8.Regex Phone number
17.8.1.Regular Expression: phone number
17.8.2.Match a phone number
17.8.3.Match phone numbers