Saturday, 31 October 2015

Write a Program in C# using Regular Expressions to read IP Address from the user and validate it.

using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        Console.WriteLine("Enter Your IP Address");
        string Ip = Console.ReadLine();

        string ipregex= @"^(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|25[0-5]|2[0-4]\d)$";

        if (Regex.IsMatch(Ip, ipregex))
        {
            Console.WriteLine("You entered ip address in correct format");
        }
        else
        {
            Console.WriteLine("ip address in invalid format");
        }
    }
}

No comments:

Post a Comment