110,502
社区成员
发帖
与我相关
我的任务
分享
/// <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;
}
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://");
Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
MessageBox.Show(reg.IsMatch("http://a.com").ToString());
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
string urlFormat = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; //定义规则
Match url = Regex.Match(tbxUrl.Text, urlFormat); //获取正则匹配的结果
if (url.Success) //判断并输出验证结果
{
MessageBox.Show("yes");
}
else
{
MessageBox.Show("no");
}