论坛,正则表达式

paigudw 2009-06-28 11:10:54
现在做了一个简易的留言板系统
求将
"[url=https://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=56eedeb3-d039-46aa-8bdd-33a5c45be40e]"

转化为
<a href="https://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=56eedeb3-d039-46aa-8bdd-33a5c45be40e" target="_blank">


的正则表达式。。
或者直接提供这种替换的完整代码也可以
谢谢!

附:自己写的一部分。

#region 过滤字符串,使输入字符串能够完美显示在网页上 Filter2(string s)
/// <summary>
/// 过滤字符串,使输入字符串能够完美显示在网页上(包含论坛字符)
/// </summary>
/// <param name="s">要转化的字符串</param>
/// <returns></returns>
public static string Filter2(string s)
{
//将&字符转义
s = s.Replace("&", "&");
//将<字符转义
s = s.Replace("<", "<");
//将>字符转义
s = s.Replace(">", ">");
//将"字符转义
s = s.Replace("\"", """);
//将空格字符转义
s = s.Replace(" ", " ");
s = s.Replace(" ", " ");
//将换行字符转义
s = s + "<p>";
s = s + "</p>";
s = s.Replace(Environment.NewLine, "</p><p>");
//转义论坛图片符号
s=Regex.Replace(s, "\\[img\\]", "<img alt=\"\" src=\"");
s = Regex.Replace(s, "\\[/img\\]", "\"/>");
//转义论坛粗体符号
s = Regex.Replace(s, "\\[bold\\]", "<span style=\"font-weight:bold;\">");
s = Regex.Replace(s, "\\[/bold\\]", "</span>");
//转义论坛链接符号
//这里不知道怎么写
s = Regex.Replace(s, "\\[/url\\]", "</a>");
return s;
}
#endregion


在线等
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
flybird066 2009-06-28
  • 打赏
  • 举报
回复
这个问题为什么要搞得那么麻烦呢,把[url=之后的部分全部取出来,去掉最后的],然后添加到<a herf="" tar……中来不就完了吗?
paigudw 2009-06-28
  • 打赏
  • 举报
回复
谢谢!二楼的方法非常好用。
同时谢谢一楼了三楼的热心!
paigudw 2009-06-28
  • 打赏
  • 举报
回复
谢谢 我试试
十八道胡同 2009-06-28
  • 打赏
  • 举报
回复
提取路径
static void Main(string[] args)
{
string mm = @"[url=https://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=56eedeb3-d039-46aa-8bdd-33a5c45be40e]";
Regex m = new Regex(@"\[url=(.*)\]");
if (m.Match(mm).Success)
Console.WriteLine(m.Match(mm).Value);
}
-过客- 2009-06-28
  • 打赏
  • 举报
回复
try...

string test = "[url=https://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=56eedeb3-d039-46aa-8bdd-33a5c45be40e]";
string result = Regex.Replace(test, @"(?i)\[url=([^\]]*)\]", @"<a href=""$1"" target=""_blank"">");
richTextBox2.Text = result;
十八道胡同 2009-06-28
  • 打赏
  • 举报
回复
思路先把路径提取出来,而后直接replace

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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