最难的正则表达式:匹配Url,解决的就给分

wzh307 2007-10-12 10:56:31
我测试的方法:
在页面有两个TextBox,一个Button

在第一个textBox中输入以下内容:

(测试内容)如果在www.baidu.com搜索csdn会出现csdn网址http://www.csdn.net/,
里面这个http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html是我的帖子

点Button 调用下面的方法:在第2个textbox 中输出结果。
其中www.baidu.com、http://www.csdn.net/、http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html是有效网址,在第2个textbox 中要加上<a>标记,

第2个textbox 中应该得到的结果:

(测试内容)如果在<a href='http://www.baidu.com'>www.baidu.com</a>搜索csdn会出现csdn网址<a href='http://www.csdn.net/'>http://www.csdn.net/</a>,
里面这个<a href='http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html'>http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html</a>是我的帖子


但我做的时候是错误的,正则表达式肯定是错误的,但我测试的方法是不是不对啊 ????

protected void btnOK_Click(object sender, EventArgs e)
{
this.txtUrl.Text = IsHavaUrl(this.TextBox1.Text);
}
调用的方法如下:

private string IsHavaUrl(string sDetail)
{
Regex r;
Match m;
r = new Regex(@"((http(s)?://([\w-]+\.)+[\w-]+(/[\w-] ./?%&=]*)?))",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(), " <A href=\"" + m.Groups[0].ToString() + "\" target=\"_blank\" >" + m.Groups[0].ToString() + " </A >");
}

return sDetail;
}


求正确的正则表达式????~~~~
...全文
1514 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzh307 2007-10-12
  • 打赏
  • 举报
回复
chuxue1342 : "(http(s)?://)?(w{3})?(\.[^.]*)*"

测试的时候报错:

“/TextBoxTest”应用程序中的服务器错误。
--------------------------------------------------------------------------------

字符串的长度不能为零。
参数名: oldValue
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentException: 字符串的长度不能为零。
参数名: oldValue

源错误:


行 156: for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
行 157: {
行 158: sDetail = sDetail.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[0].ToString() + "\" target=\"_blank\">" + m.Groups[0].ToString() + "</A>");
行 159: }
行 160:

wzh307 2007-10-12
  • 打赏
  • 举报
回复
我倒,难道我的表达有问题,

我的问题就是要用一个正则表达式,把以下内容:

(测试内容)如果在www.baidu.com搜索csdn会出现csdn网址http://www.csdn.net/,
里面这个http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html是我的帖子


转换为:

(测试内容)如果在 <a href= 'http://www.baidu.com ' >www.baidu.com </a >搜索csdn会出现csdn网址 <a href= 'http://www.csdn.net/ ' >http://www.csdn.net/ </a >,
里面这个 <a href= 'http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html ' >http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html </a >是我的帖子

OK~~~~

我倒啊~~~
chuxue1342 2007-10-12
  • 打赏
  • 举报
回复
"(http(s)?://)?(w{3})?(\.[^.]*)*"
RexZheng 2007-10-12
  • 打赏
  • 举报
回复
看你的帖子还真累

猜了半天都不知道你说的重点是什么
smallfz 2007-10-12
  • 打赏
  • 举报
回复
string text="如果在www.baidu.com搜索csdn会出现csdn网址http://www.csdn.net/?id=22,里面这个http://topic.csdn.net/u/20071009/18/76cbd513-56f8-4a1b-a555-a17b7f33c50e.html是我的帖子";
string pt=@"(www\.[!-~]+)|(https?://[!-~]+)";
string result = System.Text.RegularExpressions.Regex.Replace(text, pt, "<a href=$0 target=blank>$0</a>");


不能完全满足要求,希望有参考价值,
学习正则ing。。。。。
cyc09156 2007-10-12
  • 打赏
  • 举报
回复
最难,?我晕。。
jetxia 2007-10-12
  • 打赏
  • 举报
回复
up

110,539

社区成员

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

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

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