62,046
社区成员
发帖
与我相关
我的任务
分享
//判断方法
private int checkType(string src)
{
Regex reg = new Regex(@"(?i)^http://www\.xx\.cn/(\d+)(?:/([a-z]+)(_\d+)?(_\d+)?)?$");
Match m = reg.Match(src);
if (m.Success)
{
if (m.Groups[4].Value != "")
return 3;
else if (m.Groups[3].Value != "")
return 4;
else if (m.Groups[2].Value != "")
return 2;
else if (m.Groups[1].Value != "")
return 1;
}
return 0;
}
//调用
string[] urls = new string[] { "http://www.xx.cn/123", "http://www.xx.cn/123/blog", "http://www.xx.cn/123/view_1_234", "http://www.xx.cn/123/class_1" };
foreach (string s in urls)
{
richTextBox2.Text += "url:" + s.PadRight(35, ' ') + checkType(s) + "\n";
}
//输出
url:http://www.xx.cn/123 1
url:http://www.xx.cn/123/blog 2
url:http://www.xx.cn/123/view_1_234 3
url:http://www.xx.cn/123/class_1 4
http://www\.xx\.cn/\d+(?:/view(?:_\d+)*)?(\?(?:[^&]+&)*[^&]+)?
public bool ValidateUrl(string _strUrl)
{
string patten = @"^http://(www\.){0,1}.+\.(com|net|cn)$"; //正则表达式
Regex r = new Regex(patten); //声明一个Regex对象
Match m = r.Match(_strUrl); //使用Match方法进行匹配
if(m.Success) //匹配成功
{
return true;
}
else
{
return false;
}
}