benben 发表于 2013-1-27 04:34:56

C#写的UBB代码累

参考了一些文章,整理了一下,大家可以直接拿去用吧,其实自从有了FreeTextBox这样的东东出现,UBB已经渐渐淡出江湖了。
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Test.Com
{
/// <summary>
/// 功能:UBB代码
/// 作者:Rexsp
/// 日期:2004-4-6
/// </summary>
public class UBB
{
#region 构造函数
public UBB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
#region 公共静态方法
/// <summary>
/// UBB代码处理函数
/// </summary>
/// <param name="sDetail">输入字符串</param>
/// <returns>输出字符串</returns>
public static string UBBToHTML(string sDetail)
{
Regex r;
Match m;
#region 处理空格
sDetail = sDetail.Replace(" "," ");
#endregion
#region html标记符
sDetail = sDetail.Replace("<","<");
sDetail = sDetail.Replace(">",">");
#endregion
#region 处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/b\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<B>" + m.Groups.ToString() + "</B>");
}
#endregion
#region 处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/i\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<I>" + m.Groups.ToString() + "</I>");
}
#endregion
#region 处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/U\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<U>" + m.Groups.ToString() + "</U>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"((\r\n)*\)(.*?)((\r\n)*\[\/p\])",RegexOptions.IgnoreCase|RegexOptions.Singleline);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<P class=\"pstyle\">" + m.Groups.ToString() + "</P>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/sup\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<SUP>" + m.Groups.ToString() + "</SUP>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/sub\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<SUB>" + m.Groups.ToString() + "</SUB>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<A href=\"" + m.Groups.ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/url.gif\">" +
m.Groups.ToString() + "</A>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\+)\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<A href=\"" + m.Groups.ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/url.gif\">" +
m.Groups.ToString() + "</A>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\)([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<A href=\"mailto:" + m.Groups.ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/email.gif\">" +
m.Groups.ToString() + "</A>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\+)\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<A href=\"mailto:" + m.Groups.ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/email.gif\">" +
m.Groups.ToString() + "</A>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\)\])([ \S\t]*?)(\[\/size\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<FONT SIZE=" + m.Groups.ToString() + ">" +
m.Groups.ToString() + "</FONT>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\+)\])([ \S\t]*?)(\[\/color\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<FONT COLOR=" + m.Groups.ToString() + ">" +
m.Groups.ToString() + "</FONT>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\+)\])([ \S\t]*?)(\[\/font\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<FONT FACE=" + m.Groups.ToString() + ">" +
m.Groups.ToString() + "</FONT>");
}
#endregion
#region 处理图片链接
//处理图片链接
r = new Regex("\\(\\d+?)\\[\\/picture\\]",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + m.Groups.ToString() +
"\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups.ToString() +
"\"></A>");
}
#endregion
#region 处理
//处理
r = new Regex(@"(\+)\])([ \S\t]*?)(\[\/align\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<P align=" + m.Groups.ToString() + ">" +
m.Groups.ToString() + "</P>");
}
#endregion
#region 处标记
//处标记
r = new Regex(@"(\)\])([ \S\t]*?)(\[\/H\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),
"<H" + m.Groups.ToString() + ">" +
m.Groups.ToString() + "</H" + m.Groups.ToString() + ">");
}
#endregion
#region 处理
[*]

//处理
[*]

r = new Regex(@"(\([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
string strLI = m.Groups.ToString();
Regex rLI = new Regex(@"\[\*\]([ \S\t]*\r\n?)",RegexOptions.IgnoreCase);
Match mLI;
for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
{
strLI = strLI.Replace(mLI.Groups.ToString(),"<LI>" + mLI.Groups);
}
sDetail = sDetail.Replace(m.Groups.ToString(),
"<UL TYPE=\"" + m.Groups.ToString() + "\"><B>" + m.Groups.ToString() + "</B>" +
strLI + "</UL>");
}
#endregion
#region 处理换行
//处理换行,在每个新行的前面添加两个全角空格
r = new Regex(@"(\r\n(( )| )+)(?<正文>\S+)",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups.ToString(),"<BR>  " + m.Groups["正文"].ToString());
}
//处理换行,在每个新行的前面添加两个全角空格
sDetail = sDetail.Replace("\r\n","<BR>");
#endregion
return sDetail;
}
#endregion
}
页: [1]
查看完整版本: C#写的UBB代码累