Reputation: 7250
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:
If the user selects HTTPS from the combobox, the URL string:
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?
Upvotes: 0
Views: 2032
Reputation: 627343
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})$";
Upvotes: 3