那们老大帮忙给发一份UBB的替换正则表达式,谢谢了!

随缘而聚 2008-03-27 01:58:52
现在在要在C#中实现UBB的替换成HTML,我的正则表达式写的不好,有的内容无法正确替换,老大帮忙一下,谢谢了!

[b]测试一下UBB的格式替换[/b
...全文
73 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
随缘而聚 2008-03-28
  • 打赏
  • 举报
回复
谢谢tiantian1980!
tiantian1980 2008-03-28
  • 打赏
  • 举报
回复
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<TABLE WIDTH=" + match.Groups[2].ToString() + " STYLE=FILTER:GLOW(COLOR=" + match.Groups[3].ToString() + ", STRENGTH=" + match.Groups[4].ToString() + ")>" + match.Groups[5].ToString() + "</TABLE>");
}
regex = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<CENTER>" + match.Groups[2].ToString() + "</CENTER>");
}
regex = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \S\t]*?)(\[\/IMG\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "chr(13)&Chr(10)<a onfocus=this.blur() href=" + match.Groups[2].ToString() + "://" + match.Groups[3].ToString() + " target=_blank><IMG SRC=" + match.Groups[2].ToString() + "://" + match.Groups[3].ToString() + " border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
}
regex = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<img src=pic/em" + match.Groups[2].ToString() + ".gif border=0 align=middle>");
}
regex = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<a href=" + match.Groups[4].ToString() + " TARGET=_blank><IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a>chr(13)&Chr(10)chr(13)&Chr(10)<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + "><PARAM NAME=movie value=" + match.Groups[4].ToString() + "><PARAM NAME=quality value=high><param name=menu value=false><embed src=" + match.Groups[4].ToString() + " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + ">" + match.Groups[4].ToString() + "</embed></OBJECT>");
}
regex = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + "><param name=src value=" + match.Groups[4].ToString() + "><embed src=" + match.Groups[4].ToString() + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + "></embed></object>");
}
regex = new Regex(@"(\[rm=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + "><PARAM NAME=SRC value=" + match.Groups[4].ToString() + "><PARAM NAME=CONSOLE value=Clip1><PARAM NAME=CONTROLS value=imagewindow><PARAM NAME=AUTOSTART value=true></OBJECT>chr(13)&Chr(10)<OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + match.Groups[2].ToString() + "><PARAM NAME=SRC value=" + match.Groups[4].ToString() + "><PARAM NAME=AUTOSTART value=-1><PARAM NAME=CONTROLS value=controlpanel><PARAM NAME=CONSOLE value=Clip1></OBJECT>");
}
regex = new Regex(@"(\[mp=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + " ><param name=ShowStatusBar value=-1><param name=Filename value=" + match.Groups[4].ToString() + "><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" + match.Groups[4].ToString() + " width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + "></embed></object>");
}
regex = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<embed src=" + match.Groups[4].ToString() + " width=" + match.Groups[2].ToString() + " height=" + match.Groups[3].ToString() + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
}
regex = new Regex(@"(\[QUOTE\])([ \S\t]*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<TABLE cellSpacing=0 cellPadding=0 width=95% bgColor='#e4e4e4' border=0><TR><TD style='PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; COLOR: #999999; PADDING-TOP: 10px'>" + match.Groups[2].ToString() + "</TD></TR></TABLE>chr(13)&Chr(10)");
}
regex = new Regex(@"(\[move\])([ \S\t]*?)(\[\/move\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + match.Groups[2].ToString() + "</MARQUEE>");
}
regex = new Regex(@"(\[FLY\])([ \S\t]*?)(\[\/FLY\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + match.Groups[2].ToString() + "</MARQUEE>");
}
regex = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<br><a href=" + match.Groups[2].ToString() + " target=_blank><img src=\"" + match.Groups[2].ToString() + "\" border=0 align=middle></a><br><br>");
}
return sDetail;
}
}
tiantian1980 2008-03-28
  • 打赏
  • 举报
回复
public class UBBChange
{
public UBBChange()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
// UBB代码是HTML的一种变体,例如超链接UBB这样表述的http://www.51766.com
public static string UBBToHTML(string sDetail)
{
Match match;
sDetail = sDetail.Replace(" ", " ");
sDetail = sDetail.Replace("'", "");
sDetail = sDetail.Replace("\"", "");
sDetail = sDetail.Replace("<", "〈");
sDetail = sDetail.Replace(">", "〉");
Regex regex = new Regex(@"(\r\n(( )| )+)(?<正文>\S+)", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<BR>  " + match.Groups["正文"].ToString());
}
sDetail = sDetail.Replace("\r\n", "<BR>");
regex = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<B>" + match.Groups[2].ToString() + "</B>");
}
regex = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<I>" + match.Groups[2].ToString() + "</I>");
}
regex = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<U>" + match.Groups[2].ToString() + "</U>");
}
regex = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", RegexOptions.Singleline | RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<P class=\"pstyle\">" + match.Groups[3].ToString() + "</P>");
}
regex = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<SUP>" + match.Groups[2].ToString() + "</SUP>");
}
regex = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<SUB>" + match.Groups[2].ToString() + "</SUB>");
}
regex = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<A href=\"" + match.Groups[2].ToString() + "\" target=\"_blank\">" + match.Groups[2].ToString() + "</A>");
}
regex = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<A href=\"" + match.Groups[2].ToString() + "\" target=\"_blank\">" + match.Groups[3].ToString() + "</A>");
}
regex = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<A href=\"mailto:" + match.Groups[2].ToString() + "\" target=\"_blank\">" + match.Groups[2].ToString() + "</A>");
}
regex = new Regex(@"(\+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<A href=\"mailto:" + match.Groups[2].ToString() + "\" target=\"_blank\">" + match.Groups[3].ToString() + "</A>");
}
regex = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<FONT SIZE=" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + "</FONT>");
}
regex = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<FONT COLOR=" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + "</FONT>");
}
regex = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<FONT FACE=" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + "</FONT>");
}
regex = new Regex(@"\[picture\](\d+?)\[\/picture\]", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + match.Groups[1].ToString() + "\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + match.Groups[1].ToString() + "\"></A>");
}
regex = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<P align=" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + "</P>");
}
regex = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<H" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + "</H" + match.Groups[2].ToString() + ">");
}
regex = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
string input = match.Groups[5].ToString();
Regex regex2 = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
for (Match match2 = regex2.Match(input); match2.Success; match2 = match2.NextMatch())
{
input = input.Replace(match2.Groups[0].ToString(), "<LI>" + match2.Groups[1]);
}
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<UL TYPE=\"" + match.Groups[3].ToString() + "\"><B>" + match.Groups[4].ToString() + "</B>" + input + "</UL>");
}
regex = new Regex(@"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/SHADOW\])", RegexOptions.IgnoreCase);
for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
{
sDetail = sDetail.Replace(match.Groups[0].ToString(), "<TABLE WIDTH=" + match.Groups[2].ToString() + " STYLE=FILTER:SHADOW(COLOR=" + match.Groups[3].ToString() + "\tSTRENGTH=" + match.Groups[4].ToString() + ")>" + match.Groups[5].ToString() + "</TABLE>");
}
regex = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
tiantian1980 2008-03-27
  • 打赏
  • 举报
回复
明天到公司后给你,你留个邮箱吧
harryheart 2008-03-27
  • 打赏
  • 举报
回复
Regex.Replace("[u][i][b]",@"\[",@"\[/");
下班了,明天在看~
harryheart 2008-03-27
  • 打赏
  • 举报
回复
在[后面加/?
WNASP 2008-03-27
  • 打赏
  • 举报
回复
help up!

62,046

社区成员

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

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

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

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