主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:198次

最近我又开始用c#做论坛,在网上找了一下,虽然也有关于ubb(c#)的转换代码,可是同样都不全面,我在这里补充了一下,拿出来和大家共享。
有什么问题到我个人的论坛www.hushiyu.com来交流,随时欢迎。

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace myluntan
{
 ///
 /// UBB 的摘要说明。
 ///

 public class UBB
 {
  public UBB()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
   
  #region 公共静态方法
  ///
  /// UBB代码处理函数
  ///

  /// 输入字符串
  /// 输出字符串
  public string UBBToHTML(string sDetail)
  {
   Regex r;
   Match m;
   #region 处理空格
   sDetail = sDetail.Replace(" "," ");
   #endregion
   #region 处理单引号
   sDetail = sDetail.Replace("'","’");
   #endregion
   #region 处理双引号
   sDetail = sDetail.Replace("\"",""");
   #endregion
   #region html标记符
   sDetail = sDetail.Replace("<","<");
   sDetail = sDetail.Replace(">",">");
   
   #endregion
   #region 处理换行
   //处理换行,在每个新行的前面添加两个全角空格
   r = new Regex(@"(\r\n(( )| )+)(?<正文> +)",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
   #region 处[b][/b]标记
   r = new Regex(@"(\[b\])([ \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\])([ \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\])([ \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\])([ \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\])([ \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\])([ \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=([ \t]+)\])([ \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\])([ \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=([ \t]+)\])([ \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])\])([ \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=([ ]+)\])([ \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=([ ]+)\])([ \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(),
     "     "\" target=\"_blank\">     "\">");
   }
   #endregion
   #region 处理[align=x][/align]
   //处理[align=x][/align]
   r = new Regex(@"(\[align=([ ]+)\])([ \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])\])([ \t]*?)(\[\/H\])",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 处理[list=x][*][/list]
   //处理[list=x][*][/list]
   r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \t]*)\r\n)((\[\*\]([ \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(@"\[\*\]([ \t]*\r\n?)",RegexOptions.IgnoreCase);
    Match mLI;
    for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
    {
     strLI = strLI.Replace(mLI.Groups[0].ToString(),"
  • " + mLI.Groups[1]);
        }
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "
      " + m.Groups[4].ToString() + "" +
           strLI + "
    ");
       }

       #endregion
       #region 处[SHADOW=x][/SHADOW]标记
       //处[SHADOW=x][/SHADOW]标记
       r = new Regex(@"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([ \t]*?)(\[\/SHADOW\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "" +
         m.Groups[5].ToString() + "
    ");
       }
       #endregion
       #region 处[glow=x][/glow]标记
       //处[glow=x][/glow]标记
       r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([ \t]*?)(\[\/glow\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "" +
         m.Groups[5].ToString() + "
    ");
       }
       #endregion
       #region 处[center][/center]标记
       r = new Regex(@"(\[center\])([ \t]*?)(\[\/center\])",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 处[IMG][/IMG]标记
       r = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \t]*?)(\[\/IMG\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),"
    按此在新窗口浏览图片");
       }
       #endregion
       #region 处[em]标记
       r = new Regex(@"(\[em([ \t]*?)\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),"");
       }
       #endregion
       #region 处[flash=x][/flash]标记
       //处[mp=x][/mp]标记
       r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([ \t]*?)(\[\/flash\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "点击开新窗口欣赏该FLASH动画! [全屏欣赏]

    " + m.Groups[4].ToString() + "");
       }
       #endregion
       #region 处[dir=x][/dir]标记
       //处[dir=x][/dir]标记
       r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([ \t]*?)(\[\/dir\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "");
       }
       #endregion
       #region 处[rm=x][/rm]标记
       //处[rm=x][/rm]标记
       r = new Regex(@"(\[rm=)(\d*?),(\d*?)\]([ \t]*?)(\[\/rm\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "
    ");
       }
       #endregion
       #region 处[mp=x][/mp]标记
       //处[mp=x][/mp]标记
       r = new Regex(@"(\[mp=)(\d*?),(\d*?)\]([ \t]*?)(\[\/mp\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "");
       }
       #endregion
       #region 处[qt=x][/qt]标记
       //处[qt=x][/qt]标记
       r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([ \t]*?)(\[\/qt\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "");
       }
       #endregion
       #region 处

    标记
       r = new Regex(@"(\[QUOTE\])([ \t]*?)(\[\/QUOTE\])",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 处标记
       r = new Regex(@"(\[move\])([ \t]*?)(\[\/move\])",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 处标记
       r = new Regex(@"(\[FLY\])([ \t]*?)(\[\/FLY\])",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 处[image][/image]标记
       //处[image][/image]标记
       r = new Regex(@"(\[image\])([ \t]*?)(\[\/image\])",RegexOptions.IgnoreCase);
       for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
       {
        sDetail = sDetail.Replace(m.Groups[0].ToString(),
         "
    ");
       }
       #endregion

       return sDetail;
      }
      #endregion
     }
    }



    <> <>
  • 关键字词: