Click here to Skip to main content
15,886,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I need a small help in creating a regular expression for IPV4 address validation.
My validation string is as below..
XML
http://IP:PORT/

What I have tried is given below...
C#
private bool IsUrlValid(string url)
 {
   Regex urlRx = new Regex(@"^https?://(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/$", RegexOptions.IgnoreCase);
  return urlRx.IsMatch(url);
 }


With this code, I could perfectly validate an IPV4 address without port; But I want to modify this to accept port number also.
Any help on this would be greatly appreciated.
Thanks in advance
Posted

1 solution

Modified the expression as shown below and solved this case...

C#
private bool IsUrlValid(string url)
{
    Regex urlRx = new Regex(@"^https?://(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):?(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})/$", RegexOptions.IgnoreCase);
    return urlRx.IsMatch(url);
}


Thanks
Sebastian
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900