正文

C#写的UBB代码类2006-05-11 17:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/13920.html

分享到:

参考了一些文章,整理了一下,大家可以直接拿去用吧,其实自从有了FreeTextBox这样的东东出现,UBB已经渐渐淡出江湖了。 using System; using System.Text; using System.Text.RegularExpressions; namespace Test.Com { ///

/// 功能:UBB代码 /// 作者:Rexsp /// 日期:2004-4-6 /// public class UBB { #region 构造函数 public UBB() { // // TODO: 在此处添加构造函数逻辑 // } #endregion #region 公共静态方法 /// /// UBB代码处理函数 /// /// 输入字符串 /// 输出字符串 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(),"" + m.Groups[2].ToString() + ""); } #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(),"" + m.Groups[2].ToString() + ""); } #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(),"" + m.Groups[2].ToString() + ""); } #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(),"

" + m.Groups[3].ToString() + "

"); } #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(),"" + m.Groups[2].ToString() + ""); } #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(),"" + m.Groups[2].ToString() + ""); } #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(), "" + m.Groups[2].ToString() + ""); } #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(), "" + m.Groups[3].ToString() + ""); } #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(), "" + m.Groups[2].ToString() + ""); } #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(), "" + m.Groups[3].ToString() + ""); } #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(), "" + m.Groups[3].ToString() + ""); } #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(), "" + m.Groups[3].ToString() + ""); } #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(), "" + m.Groups[3].ToString() + ""); } #endregion #region 处理图片链接 //处理图片链接 r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]",RegexOptions.IgnoreCase); for (m = r.Match(sDetail); m.Success; m = m.NextMatch()) { sDetail = sDetail.Replace(m.Groups[0].ToString(), ""); } #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(), "

" + m.Groups[3].ToString() + "

"); } #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(), "" + mLI.Groups[1]); } sDetail = sDetail.Replace(m.Groups[0].ToString(), "
    " + m.Groups[4].ToString() + "" + strLI + "
"); } #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[0].ToString(),"
  " + m.Groups["正文"].ToString()); } //处理换行,在每个新行的前面添加两个全角空格 sDetail = sDetail.Replace("\r\n","
"); #endregion return sDetail; } #endregion } }

阅读(2389) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册