请问在前台多行文本框中输入UBB代码,在后台用C#是怎么样将UBB转换为HTML的,我用
 
public string ubbtohtml(string sDetail)
{
   Regex r;
   Match m;
   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(),"<B>" + m.Groups[2].ToString() + "</B>");
   }
 
   return sDetail;
}

这样的代码,可是一点作用也没有。还有C#中将UBB转换为HTML的正则表达式都是怎么写的,会不会是我的正则表达式写错了?有谁知道这几个问题怎么解的,帮帮我,谢谢。

解决方案 »

  1.   

    Regex.Replace(str, @"\[b\](.+?)\[/b\]", "<b>$1</b>");
      

  2.   

    部分代码:#region 处<strong></strong>标记
       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(),"<B>" + m.Groups[2].ToString() + "</B>");
       }
       #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(),"<I>" + m.Groups[2].ToString() + "</I>");
       }
       #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(),"<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(),"<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
       }
       #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(),"<SUP>" + m.Groups[2].ToString() + "</SUP>");
       }
       #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(),"<SUB>" + m.Groups[2].ToString() + "</SUB>");
       }
       #endregion
       #region 处标记
       //处标记
       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(),
         "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
         m.Groups[2].ToString() + "</A>");
       }
       #endregion
       #region 处xxx标记
       //处xxx标记
       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(),
         "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
         m.Groups[3].ToString() + "</A>");
       }
       #endregion
      

  3.   

    感谢NqIceCoffee,你的方法是正确的;to zhoufoxcn:
      也感谢zhoufoxcn,不过你发的这些代码是错的,我原先的代码就是这些,事实上里面正则表达式都是错的。