0
votes

I'm working on an application that needs to validate a user-entered URL string. The user selects the protocol (either HTTPS, TCP or UDP) from a combobox. If they select TCP or UDP from the combobox, the URL string:

  • Must be entered as either an IPv4 address or hostname
  • Must contain a valid port number (1 - 65535)
  • In the case of IPv4 addresses, each octet must be valid (between 0 and 255)

If the user selects HTTPS from the combobox, the URL string:

  • Must be entered as either an IPv4 address or hostname
  • May contain a path
  • May contain a valid port number (1 - 65535)
  • In the case of IPv4 addresses, each octet must be valid (between 0 and 255)

I've taken a stab at constructing a couple of regular expressions to accomplish validation of the URL strings described above but unfortunately, I seem to have something wrong. I'm far from an expert at regular expressions and I've looked at these for so long now, I think I need some assistance from someone smarter than myself. :-)

Here are a few sample strings that should be valid:

For TCP/UDP:
10.1.1.100:12345
mydomain.fr:31337

For HTTPS:
192.168.0.1
192.168.0.1/testdir
192.168.0.1:12345/testdir
192.168.0.1:12345/testdir/testdir_2
mydomain.fr
www.mydomain.fr
www.mydomain.fr/testdir
www.mydomain.fr:12345/testdir
www.mydomain.fr:12345/testdir/testdir_2

Here are the two C# methods that I'm trying to use to validate the URL string:

public static bool IsValidHttpsUrl(string url)
{
    const string Pattern = @"^(([a-z0-9-]+\\.)+[a-z]{2,6}|((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}))?(/[\\w/.]*)?$";

    var regex = new System.Text.RegularExpressions.Regex(Pattern, System.Text.RegularExpressions.RegexOptions.Compiled);
    return regex.Match(url).Success;
}

public static bool IsValidTcpOrUdpUrl(string url)
{
    const string Pattern = @"^(([a-z0-9-]+\\.)+[a-z]{2,6}|((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})$";

    var regex = new System.Text.RegularExpressions.Regex(Pattern, System.Text.RegularExpressions.RegexOptions.Compiled);
    return regex.Match(url).Success;
}

Can anyone shed some light on where I've gone wrong?

1
You double escaped backslashes inside a verbatim string literal. Use single backslashes. See demoWiktor Stribiżew
But what's the problem with your code=Gusman
@WiktorStribiżew Doh!!! You're right. I can't believe I missed that. Thanks for pointing out my stupid mistake. :-)bmt22033
Considered Uri.TryCreate?Alex K.

1 Answers

3
votes

You just need to either use a regular string literal for regular expression declaration (remove @), or use single backslashes.

const string Pattern = @"^(([a-z0-9-]+\.)+[a-z]{2,6}|((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}))?(/[\w/.]*)?$";

const string Pattern = @"^(([a-z0-9-]+\.)+[a-z]{2,6}|((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})$";