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






using System;
using System.Text.RegularExpressions;

  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
        Regex phoneExp = new Regex( @"^\(\d{3}\)\s\d{3}-\d{4}$" );
          string input;

            Console.Write( "Enter a phone number: " );
            input = Console.ReadLine();
            
            while( phoneExp.Match( input ).Success == false )
            {
                Console.WriteLine( "Invalid input. Try again." );
                Console.Write( "Enter a phone number: " );
                input = Console.ReadLine();
            }

            Console.WriteLine( "Validated!" );
    }
  }








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