参考了一些文章,整理了一下,大家可以直接拿去用吧,其实自从有了FreeTextBox这样的东东出现,UBB已经渐渐淡出江湖了。
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Test.Com
{
///
1<summary>
2/// 功能:UBB代码
3/// 作者:Rexsp
4/// 日期:2004-4-6
5/// </summary>
public class UBB
{
#region 构造函数
public UBB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
#region 公共静态方法
///
1<summary>
2/// UBB代码处理函数
3/// </summary>
///
1<param name="sDetail"/>
输入字符串
///
1<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 处[b][/b]标记
r = new Regex(@"(\[b\])([ \S\t]*?)(\[/b\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
1<b>" + m.Groups[2].ToString() + "</b>
");
}
#endregion
#region 处[i][/i]标记
r = new Regex(@"(\[i\])([ \S\t]*?)(\[/i\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
1<i>" + m.Groups[2].ToString() + "</i>
");
}
#endregion
#region 处[u][/u]标记
r = new Regex(@"(\[U\])([ \S\t]*?)(\[/U\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
1<u>" + m.Groups[2].ToString() + "</u>
");
}
#endregion
#region 处[p][/p]标记
//处[p][/p]标记
r = new Regex(@"((\r\n)\[p\])(.?)((\r\n)*\[/p\])",RegexOptions.IgnoreCase|RegexOptions.Singleline);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
1<p class='\"pstyle\"'>" + m.Groups[3].ToString() + "</p>
");
}
#endregion
#region 处[sup][/sup]标记
//处[sup][/sup]标记
r = new Regex(@"(\[sup\])([ \S\t]*?)(\[/sup\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
1<sup>" + m.Groups[2].ToString() + "</sup>
");
}
#endregion
#region 处[sub][/sub]标记
//处[sub][/sub]标记
r = new Regex(@"(\[sub\])([ \S\t]*?)(\[/sub\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
1<sub>" + m.Groups[2].ToString() + "</sub>
");
}
#endregion
#region 处[url][/url]标记
//处[url][/url]标记
r = new Regex(@"(\[url\])([ \S\t]*?)(\[/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<a "\"="" +="" href='\""' m.groups[2].tostring()="" target='\"_blank\"'><img border="0" src='\"images/url.gif\"'/>" +
2m.Groups[2].ToString() + "</a>
");
}
#endregion
#region 处[url=xxx][/url]标记
//处[url=xxx][/url]标记
r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<a "\"="" +="" href='\""' m.groups[2].tostring()="" target='\"_blank\"'><img border="0" src='\"images/url.gif\"'/>" +
2m.Groups[3].ToString() + "</a>
");
}
#endregion
#region 处[email][/email]标记
//处[email][/email]标记
r = new Regex(@"(\[email\])([ \S\t]*?)(\[/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<a "\"="" +="" href='\"mailto:"' m.groups[2].tostring()="" target='\"_blank\"'><img border="0" src='\"images/email.gif\"'/>" +
2m.Groups[2].ToString() + "</a>
");
}
#endregion
#region 处[email=xxx][/email]标记
//处[email=xxx][/email]标记
r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<a "\"="" +="" href='\"mailto:"' m.groups[2].tostring()="" target='\"_blank\"'><img border="0" src='\"images/email.gif\"'/>" +
2m.Groups[3].ToString() + "</a>
");
}
#endregion
#region 处[size=x][/size]标记
//处[size=x][/size]标记
r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[/size\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<font size=" + m.Groups[2].ToString() + ">" +
2m.Groups[3].ToString() + "</font>
");
}
#endregion
#region 处[color=x][/color]标记
//处[color=x][/color]标记
r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[/color\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<font color=" + m.Groups[2].ToString() + ">" +
2m.Groups[3].ToString() + "</font>
");
}
#endregion
#region 处[font=x][/font]标记
//处[font=x][/font]标记
r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[/font\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<font face=" + m.Groups[2].ToString() + ">" +
2m.Groups[3].ToString() + "</font>
");
}
#endregion
#region 处理图片链接
//处理图片链接
r = new Regex(" \\picture\\\\[\/picture\\]",RegexOptions.IgnoreCase );
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<a "\"="" +="" href='\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID="' m.groups[1].tostring()="" target='\"_blank\"'><img "\"="" +="" border="0" m.groups[1].tostring()="" src='\"ShowImage.aspx?Action=forumImage&ImageID="' title='\"点击打开新窗口查看\"'/></a>
");
}
#endregion
#region 处理[align=x][/align]
//处理[align=x][/align]
r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[/align\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<p align=" + m.Groups[2].ToString() + ">" +
2m.Groups[3].ToString() + "</p>
");
}
#endregion
#region 处[H=x][/H]标记
//处[H=x][/H]标记
r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[/H\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
1<h" "="" +="" m.groups[2].tostring()="">" +
2m.Groups[3].ToString() + "</h">
");
}
#endregion
#region 处理[list=x][][/list]
//处理[list=x][][/list]
r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \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[5].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[0].ToString(),"
1<li>" + mLI.Groups[1]);
2}
3
4sDetail = sDetail.Replace(m.Groups[0].ToString(),
5"<ul "\"="" +="" m.groups[3].tostring()="" type='\""'><b>" + m.Groups[4].ToString() + "</b>" +
6strLI + "</ul>");
7}
8#endregion
9
10#region 处理换行
11//处理换行,在每个新行的前面添加两个全角空格
12r = new Regex(@"(\r\n(( )| )+)(?<正文>\S+)",RegexOptions.IgnoreCase);
13for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
14{
15sDetail = sDetail.Replace(m.Groups[0].ToString(),"<br/> " + m.Groups["正文"].ToString());
16}
17//处理换行,在每个新行的前面添加两个全角空格
18sDetail = sDetail.Replace("\r\n","<br/>");
19#endregion
20
21return sDetail;
22}
23#endregion
24
25}
26}</li>