Winform中如何判断url的格式是否正确啊 看到很多说正则,正则具体怎么写,求教! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public bool urlCheck(string http) { return Regex.IsMatch(http, ("^http\\")); } 这个正则是我乱整的,求正确解答! url的正则网上多的是,有简单的,有复杂的,楼主要学会搜索呀 新建一个按钮和文本框 按钮单击判断是否正确string urlFormat = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; //定义规则Match url = Regex.Match(tbxUrl.Text, urlFormat); //获取正则匹配的结果 if (url.Success) //判断并输出验证结果 { MessageBox.Show("yes"); } else { MessageBox.Show("no"); } 正则:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");MessageBox.Show(reg.IsMatch("http://a.com").ToString()); 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://"); /// <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; } 哪需要正则这么麻烦,直接用System.Uri就可以了。Uri.IsWellFormedUriString("http://host.com/page.html", UriKind.Absolute);如果需要再判断是否HTTP协议,可再使用Uri.CheckSchemeName方法验证。 http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? 楼上的都是只是判断www形式的,比如我http://localhost/XXXX或者http://192.168.1.1/xxxx就不行的啊 如何用在窗体加载时给iframe中的div加上样式 正则表达式匹配:<!-- --> dv.RowFilter = "dirname=0";老是报错,不知道为什么 asp.net播放rm文件 C#程序调用COM组件,编译好后放在其它电脑上无法运行。怎么办? 如何用C#监控注册表 如何用C#代码实现WORD的自动图文集的功能 SqlCommand 打开问题 关于C#实现三层架构的问题。 C#写的自定义控件用于网页上 我应该创建一个副本还是直接赋值?求正解! mdi子窗体
{
return Regex.IsMatch(http, ("^http\\"));
}
这个正则是我乱整的,求正确解答!
按钮单击判断是否正确
string urlFormat = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; //定义规则
Match url = Regex.Match(tbxUrl.Text, urlFormat); //获取正则匹配的结果
if (url.Success) //判断并输出验证结果
{
MessageBox.Show("yes");
}
else
{
MessageBox.Show("no");
}
Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
MessageBox.Show(reg.IsMatch("http://a.com").ToString());
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://");
/// <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;
}