Validate IP Address Using Regex

Some times we required to validate IP address of system through Regex, then this example is the solution for your requirement, See the example:

  1. public bool IsValidateIP(string Address)    
  2. {    
  3.     //Match pattern for IP address    
  4.     string Pattern = @"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$";    
  5.     //Regular Expression object    
  6.     Regex check = new Regex(Pattern);    
  7.   
  8.     //check to make sure an ip address was provided    
  9.     if (string.IsNullOrEmpty(Address))    
  10.         //returns false if IP is not provided    
  11.         return false;    
  12.     else    
  13.        //Matching the pattern    
  14.         return check.IsMatch(Address, 0);    
  15. }   
Above function is validating IP address is provided. just need to remember the pattern which takes measure role for validating IP address.