求正则表达式写法!在线等! 急!!!

AutumanChen 2006-04-12 02:54:59
求:[ATT]http://www.123.com/bbs/attdocument/abcd.txt[/ATT]

变成:“附件:<A HREF="http://www.123.com/bbs/attdocument/abcd.txt" TARGET=_blank>abcd.txt</A>”

的正则表达式写法!

谢谢!
...全文
168 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2006-06-07
  • 打赏
  • 举报
回复
sorry, I mean webwait(webwei)'s method is ok
saucer 2006-06-07
  • 打赏
  • 举报
回复
AutumanChen's method is ok, you just need do something with the url itself, for example


static string MyMatchEval(Match m)
{
/*string s = m.Groups["name"].Value;
Uri u = new Uri(s);
return String.Format("<a href=\"{0}\">{1}</a>",s,System.IO.Path.GetFileName(u.LocalPath));
*/

string s = m.Groups["name"].Value;
return String.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>",s,System.IO.Path.GetFileName(s));
}

static void Test()
{

string vstr=@"[ATT]http://www.123.com/bbs/attdocument/ccccc.txt[/ATT] [ATT]http://www.123.com/bbs/attdocument/abcd.txt[/ATT]

[ATT]http://www.123.com/bbs/attdocument/ddddd.txt[/ATT]";
vstr=Regex.Replace(vstr, @"\[ATT](?<name>.+?)\[/ATT]", new MatchEvaluator(MyMatchEval), RegexOptions.Compiled | RegexOptions.IgnoreCase);
Console.WriteLine(vstr);

}

output:

<a href="http://www.123.com/bbs/attdocument/ccccc.txt" target="_blank">ccccc.txt</a> <a href="http://www.123.com/bbs/attdocument/abcd.txt" target="_blank">abcd.txt</a> <a href="http://www.123.com/bbs/attdocument/ddddd.txt" target="_blank">ddddd.txt</a>
阿良chjlcn 2006-06-07
  • 打赏
  • 举报
回复
用MatchCollection
chenqiuyong 2006-06-07
  • 打赏
  • 举报
回复
还是建议你用 .replace() 吧!
Ninputer 2006-04-16
  • 打赏
  • 举报
回复
凡是有括号匹配规则的,比如左右括号匹配,左右[ATT][/ATT]匹配的钧不是正则语言能够表达的内容,因此用正则语言无法做到100%准确处理。你必须归纳出一些特例条件来。
AutumanChen 2006-04-15
  • 打赏
  • 举报
回复
沒人知道啊??
AutumanChen 2006-04-12
  • 打赏
  • 举报
回复
webwait(webwei) , 用你这方法,我如果只是一个附件还行,如何是多个,就不行了,因为我这儿可能会存在有: "[ATT]http://www.123.com/bbs/attdocument/ccccc.txt[/ATT] [ATT]http://www.123.com/bbs/attdocument/abcd.txt[/ATT] [ATT]http://www.123.com/bbs/attdocument/ddddd.txt[/ATT]" 等等,很多个连续的!!
webwait 2006-04-12
  • 打赏
  • 举报
回复
string vstr="[ATT]http://www.123.com/bbs/attdocument/abcd.txt[/ATT]";
vstr=Regex.Replace(vstr, @"\[ATT](?<name>.+?)\[/ATT]", "${name}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string strtemp=vstr.Split('/').GetValue(vstr.Split('/').Length-1).ToString();
Response.Write("附件:<A HREF='"+vstr+"' TARGET=_blank>"+strtemp+"</a>");

62,046

社区成员

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

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

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

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