求把ubb代码转换成html的正则表达式代码

mongmong 2004-07-12 03:40:17
哪位大虾给个范例出来?不胜感激
...全文
121 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mongmong 2004-07-14
谢谢了
回复
Alexandria 2004-07-14
好长
回复
Truly 2004-07-13

MatchCollection mc;
Regex r = new Regex(@"\[code\]([^\]]*)\[/code\]");
mc = r.Matches(chr);
// for(int i = 0; i < mc.Count; i++)
// {
// chr = chr.Replace(mc[i].ToString(),Code(mc[i].ToString()));
// }
chr = Regex.Replace(chr,@"\[code\](?<x>[^\]]*)\[/code\]",@"以下内容为程序代码:<table border='0' width='100%' style='TABLE-LAYOUT: fixed' cellpadding='10' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'>$1</td></tr></table>",RegexOptions.IgnoreCase);
r = new Regex(@"\[html\]([^\]]*)\[/html\]");
mc = r.Matches(chr);
// for(int i = 0; i < mc.Count; i++)
// {
// chr = chr.Replace(mc[i].ToString(),Html(mc[i].ToString()));
// }
chr = Regex.Replace(chr,@"\[html\](?<x>[^\]]*)\[/html\]",@"以下内容为页面代码:<table border='0' width='100%' style='TABLE-LAYOUT: fixed' cellpadding='10' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><font color='#0000FF'>$1</font></td></tr></table>",RegexOptions.IgnoreCase);
return(chr);
}
回复
Truly 2004-07-13
chr = Regex.Replace(chr,@"\[size=2\](?<x>[^\]]*)\[/size\]",@"<font size=2>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=3\](?<x>[^\]]*)\[/size\]",@"<font size=3>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=4\](?<x>[^\]]*)\[/size\]",@"<font size=4>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[align=(?<x>[^\]]*)\](?<y>[^\]]*)\[/align\]",@"<align=$1>$2</align>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[fly](?<x>[^\]]*)\[/fly]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[move](?<x>[^\]]*)\[/move]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[glow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/glow\]",@"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]",@"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[b\](?<x>[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[i\](?<x>[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[u\](?<x>[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[list\](?<x>[^\]]*)\[/list\]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[list=1\](?<x>[^\]]*)\[/list\]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[list=a\](?<x>[^\]]*)\[/list\]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[\*\](?<x>[^\]]*)\[/\*\]",@"<li>$1</li>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\
引用
(?<x>.*)\
",@"以下内容为引用:<table border='0' width='90%' style='TABLE-LAYOUT: fixed' cellpadding='10' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'>$1</td></tr></table>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[gif](?<x>[^\]]*)\[/gif]",@"<IMG SRC='Pic/Gif.gif' border=0>此主题相关图片如下:<br><a href=""$1"" target=""_blank""><IMG SRC=""$1"" border=0></a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[jpg](?<x>[^\]]*)\[/jpg]",@"<IMG SRC='Pic/Jpg.gif' border=0>此主题相关图片如下:<br><a href=""$1"" target=""_blank""><IMG SRC=""$1"" border=0></a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[zip](?<x>[^\]]*)\[/zip]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/Zip.gif' border=0>请点击此处下载</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[rar](?<x>[^\]]*)\[/rar]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/Rar.gif' border=0>请点击此处下载</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[htm](?<x>[^\]]*)\[/htm]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/Url.gif' border=0>请点击此处浏览</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[doc](?<x>[^\]]*)\[/doc]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/word.gif' border=0>请点击此处浏览</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[xls](?<x>[^\]]*)\[/xls]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/excel.gif' border=0>请点击此处浏览</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[ppt](?<x>[^\]]*)\[/ppt]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/powerpoint.gif' border=0>请点击此处浏览</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[wps](?<x>[^\]]*)\[/wps]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/wps.gif' border=0>请点击此处浏览</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[mht](?<x>[^\]]*)\[/mht]",@"<a href=""$1"" target=""_blank""><IMG SRC='Pic/mht.gif' border=0>请点击此处浏览</a><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[swf](?<x>[^\]]*)\[/swf]",@"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[mp3](?<x>[^\]]*)\[/mp3]",@"<OBJECT class=OBJECT id=MediaPlayer height=50 width=300 align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95><PARAM NAME=ShowStatusBar VALUE=-1><PARAM NAME=Filename VALUE=""$1""><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=""$1"" width=200 height=40></embed></OBJECT><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[mpg](?<x>[^\]]*)\[/mpg]",@"<OBJECT class=OBJECT id=MediaPlayer height=300 width=300 align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95><PARAM NAME=ShowStatusBar VALUE=-1><PARAM NAME=Filename VALUE=""$1""><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=""$1"" width=200 height=40></embed></OBJECT><br>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[ram](?<x>[^\]]*)\[/ram]",@"<embed src=""$1"" type=audio/x-pn-realaudio-plugin console=Clip1 controls=ControlPanel,StatusBar height=60 width=275 autostart=true>",RegexOptions.IgnoreCase);
回复
Truly 2004-07-13
public string Ubb(string chr)
{
if(chr==null)
return "";
chr=chr.Replace(" "," ");
chr=chr.Replace("<","<");
chr=chr.Replace(">",">");
chr=chr.Replace("\n","<br>");
chr=chr.Replace("\t","   ");
chr = Regex.Replace(chr,@"\[url=(?<x>[^\]]*)\](?<y>[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[url\](?<x>[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[rm=(?<x>[^\]]*)\](?<y>[^\]]*)\[/rm\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[rm\](?<x>[^\]]*)\[/rm\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
//chr = Regex.Replace(chr,@"\[mpg=(?<x>[^\]]*)\](?<y>[^\]]*)\[/mpg\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
//chr = Regex.Replace(chr,@"\[mpg\](?<x>[^\]]*)\[/mpg\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[mov=(?<x>[^\]]*)\](?<y>[^\]]*)\[/mov\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[mov\](?<x>[^\]]*)\[/mov\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\]*)\](?<y>[^\]]*)\[/email\]",@"<a href=$1>$2</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[email\](?<x>[^\]]*)\[/email\]",@"<a href=$1>$1</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[flash](?<x>[^\]]*)\[/flash]",@"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[img](?<x>[^\]]*)\[/img]",@"<div align=center><br><a href=""$1"" target=""_blank""><IMG SRC=""$1"" border=0 align='middle'></a><br></div>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[color=(?<x>[^\]]*)\](?<y>[^\]]*)\[/color\]",@"<font color=$1>$2</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[face=(?<x>[^\]]*)\](?<y>[^\]]*)\[/face\]",@"<font face=$1>$2</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=1\](?<x>[^\]]*)\[/size\]",@"<font size=1>$1</font>",RegexOptions.IgnoreCase);
回复
Bob 2004-07-12
http://dev.csdn.net/develop/article/26/26837.shtm
回复
GZ
回复
发动态
发帖子
.NET Framework
创建于2007-09-28

1.7w+

社区成员

.NET技术 .NET Framework
申请成为版主
社区公告
暂无公告