简体   繁体   English

正则表达式可验证指定为IP v4地址或主机名的字符串,带或不带端口号

[英]Regex to validate string specified as IP v4 address or hostname with or without port number

I'm working on an application that needs to validate a user-entered URL string. 我正在开发一个需要验证用户输入的URL字符串的应用程序。 The user selects the protocol (either HTTPS, TCP or UDP) from a combobox. 用户从组合框中选择协议(HTTPS,TCP或UDP)。 If they select TCP or UDP from the combobox, the URL string: 如果他们从组合框中选择TCP或UDP,则URL字符串:

  • Must be entered as either an IPv4 address or hostname 必须输入为IPv4地址或主机名
  • Must contain a valid port number (1 - 65535) 必须包含有效的端口号(1-65535)
  • In the case of IPv4 addresses, each octet must be valid (between 0 and 255) 对于IPv4地址,每个字节必须有效(0到255之间)

If the user selects HTTPS from the combobox, the URL string: 如果用户从组合框中选择HTTPS,则URL字符串:

  • Must be entered as either an IPv4 address or hostname 必须输入为IPv4地址或主机名
  • May contain a path 可能包含路径
  • May contain a valid port number (1 - 65535) 可能包含有效的端口号(1-65535)
  • In the case of IPv4 addresses, each octet must be valid (between 0 and 255) 对于IPv4地址,每个字节必须有效(0到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. 我在构造几个正则表达式以完成对上述URL字符串的验证时采取了一些措施,但是不幸的是,我似乎有问题。 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: 这是我要用来验证URL字符串的两种C#方法:

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? 谁能告诉我我哪里出问题了?

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})$";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM