鉴于此正则表达式:
^((https?|ftp):(\/{2}))?(((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]?))|(((([a-zA-Z0-9]+)(\.)*?))(\.)([a-z]{2}
|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum){1})
重新格式化以提高可读性:
@"^((https?|ftp):(\/{2}))?" + // http://, https://, ftp:// - Protocol Optional
@"(" + // Begin URL payload format section
@"((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]?)" + // IPv4 Address support
@")|("+ // Delimit supported payload types
@"((([a-zA-Z0-9]+)(\.)*?))(\.)([a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum){1}" + // FQDNs
@")"; // End URL payload format section
如何在此“失败”测试用例中使其失败(即不匹配)?
http://www.google
当我在TLD部分中指定{1}时,我认为如果没有扩展名它将失败。我错了吗?
编辑:这些是我的通过条件:
“ http://www.zi255.com?Req=Post&PID=4”,
“ http://www.zi255.com?Req=Post&ID=4”,
“ http://www.zi255.com/?Req=Post&PID=4”,
“ http://www.zi255.com?Req=Post&PostID=4”,
“ http://www.zi255.com/?Req=Post&ID=4”
“ http://www.zi255.com?Req=Post&Post=4”,
“ http://www.zi255.com?Req=Post&Entry=4”,
“ http://www.zi255.com?PID=4”
“ http://www.zi255.com/Post.aspx?Req=Post&ID=4”,
“ http://www.zi255.com/Post.aspx?Req=Post&PID=4”,
“ http://www.zi255.com/Post.aspx?Req=Post&Post=4”,
“ http://www.zi255.com/Post.aspx?Req=Post&Title=Random%20Post%20Name”
“ http://www.zi255.com/?Req=Post&Title=Random%20Post%20Name”,
“ http://www.zi255.com?Req=Post&Title=Random%20Post%20Name”,
“ http://www.zi255.com?Req=Post&PostID=4”,
“ http://www.zi255.com?Req=Post&Post=4”,
“ http://www.zi255.com?Req=Post&Entry=4”,
“ http://www.zi255.com?PID=4”
“ http://www.zi255.com”,
“ http://www.damnednice.com”
这些是我的失败条件:
“ http://.com”,
“ http://.com/”,
“ http:/www.google.com”,
“ http:/www.google.com/”,
“ http://www.google”,
“ http://www.googlecom”,
“ http://www.google.c”,
“ .com”,
“ https://www ...”
请您参考如下方法:
我会提出另一个建议。您可能需要结合使用内置的System.Uri类的解析和几个目标正则表达式(或在适当时进行简单的字符串检查)。
例:
string uriString = "...";
Uri uri;
if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri))
{
// Uri is totally invalid!
}
else
{
// validate the scheme
if (!uri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
{
// not http!
}
// validate the authority ('www.blah.com:1234' portion)
if (uri.Authority // ...)
{
}
// ...
}
