C# Regex



C# implementation of regex to check if parameter is a valid IP address.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;


    public class Program
    {
        public static string CheckValidIP(string ipaddress)
        {
        string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})$";
        Match result = Regex.Match(ipaddress, pattern);
        if (result.Success) 
        {
            return result.Value.ToString();
        }
            
        else
        {
            return "Not Valid IP!";
        }
            
        }
        
        public static void Main(string[] args)
        { 
            Console.WriteLine(CheckValidIP("255.48.8.29"));
            Console.WriteLine(CheckValidIP("999.48.8.29"));
        }
    }
    
Ian Fogelman

Ian Fogelman

My name is Ian Fogelman. I like to develop data driven solutions with SQL Server, Python, .NET and predictive analytics.