Winform中如何判断url的格式是否正确啊

Joe-Fan 2011-09-06 10:10:06
看到很多说正则,正则具体怎么写,求教!
...全文
485 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
LMAOhuaNL 2011-09-07
  • 打赏
  • 举报
回复
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Joe-Fan 2011-09-07
  • 打赏
  • 举报
回复
这个方法很好用,不错,灰常的感谢![Quote=引用 12 楼 effun 的回复:]

哪需要正则这么麻烦,直接用System.Uri就可以了。

Uri.IsWellFormedUriString("http://host.com/page.html", UriKind.Absolute);

如果需要再判断是否HTTP协议,可再使用Uri.CheckSchemeName方法验证。
[/Quote]
Joe-Fan 2011-09-07
  • 打赏
  • 举报
回复
楼上的都是只是判断www形式的,比如我http://localhost/XXXX或者http://192.168.1.1/xxxx就不行的啊[Quote=引用 14 楼 zyloveyrf 的回复:]

LZ 楼上的那么多 应该有你需要的了吧
[/Quote]
Nick黄 2011-09-07
  • 打赏
  • 举报
回复
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
lisong770818 2011-09-07
  • 打赏
  • 举报
回复
楼上很厉害
萧炎 2011-09-07
  • 打赏
  • 举报
回复
LZ 楼上的那么多 应该有你需要的了吧
波波007 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sanjiawan 的回复:]
新建一个按钮和文本框
按钮单击判断是否正确


C# code

string urlFormat = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; //定义规则
Match url = Regex.Match(tbxUrl.Text, urlFormat); //获取正则匹配的结果
if (ur……
[/Quote]

正则表达式!
effun 2011-09-07
  • 打赏
  • 举报
回复
哪需要正则这么麻烦,直接用System.Uri就可以了。

Uri.IsWellFormedUriString("http://host.com/page.html", UriKind.Absolute);

如果需要再判断是否HTTP协议,可再使用Uri.CheckSchemeName方法验证。
潘少博 2011-09-07
  • 打赏
  • 举报
回复
恩。应该把一些常用的正则表达式收藏起来!
isjoe 2011-09-07
  • 打赏
  • 举报
回复

/// <summary>
/// 检测串值是否为合法的网址格式
/// </summary>
/// <param name="strValue">要检测的String值</param>
/// <returns>成功返回true 失败返回false</returns>
public static bool CheckIsUrlFormat(string strValue)
{
return Utility.CheckIsFormat(@"(http://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", strValue);
}

/// <summary>
/// 检测串值是否为合法的格式
/// </summary>
/// <param name="strRegex">正则表达式</param>
/// <param name="strValue">要检测的String值</param>
/// <returns>成功返回true 失败返回false</returns>
public static bool CheckIsFormat(string strRegex,string strValue)
{
if(strValue != null && strValue.Trim() != "")
{
Regex re = new Regex(strRegex);
if (re.IsMatch(strValue))
{
return true;
}
else
{
return false;
}
}
return false;
}
hangang7403 2011-09-07
  • 打赏
  • 举报
回复
dingqi
xiongxyt2 2011-09-07
  • 打赏
  • 举报
回复

string reg = @"^(http(s)?:\/\/)?(www\.)?[\w-]+(\.\w{2,4})?\.\w{2,4}?(\/)?$";
Regex r = new Regex(reg);
//给网址去所有空格
string urlStr = webresourceaddress.Trim();
Match m = r.Match(urlStr);

//判断是否带http://
if (!m.Success)
return false;
//给不带http://开头的加上
urlStr = urlStr.Replace("http://", "");
urlStr = urlStr.Insert(0, "http://");


风骑士之怒 2011-09-07
  • 打赏
  • 举报
回复

Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
MessageBox.Show(reg.IsMatch("http://a.com").ToString());
暖枫无敌 2011-09-06
  • 打赏
  • 举报
回复
正则:

http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
sanjiawan 2011-09-06
  • 打赏
  • 举报
回复
新建一个按钮和文本框
按钮单击判断是否正确


string urlFormat = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; //定义规则
Match url = Regex.Match(tbxUrl.Text, urlFormat); //获取正则匹配的结果
if (url.Success) //判断并输出验证结果
{
MessageBox.Show("yes");
}
else
{
MessageBox.Show("no");
}
Joe-Fan 2011-09-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hzieechenwei 的回复:]

url的正则网上多的是,有简单的,有复杂的,楼主要学会搜索呀
[/Quote]


搜了很多,都不对啊,不然也不会问了,实在没辙了!
hzieechenwei 2011-09-06
  • 打赏
  • 举报
回复
url的正则网上多的是,有简单的,有复杂的,楼主要学会搜索呀
Joe-Fan 2011-09-06
  • 打赏
  • 举报
回复
public bool urlCheck(string http)
{
return Regex.IsMatch(http, ("^http\\"));
}
这个正则是我乱整的,求正确解答!

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧