Copy codeThe Code is as follows:
//// Ubb code written by others
Using System;
Using System. Text;
Using System. Text. RegularExpressions;
Namespace Test. Com
{
/// <Summary>
/// Function: UBB Code
/// By Rexsp
/// Date: 2004-4-6
/// </Summary>
Public class UBB
{
# Region Constructor
Public UBB ()
{
//
// TODO: add the constructor logic here
//
}
# Endregion
# Region Public static method
/// <Summary>
/// UBB code processing function
/// </Summary>
/// <Param name = "sDetail"> input string </param>
/// <Returns> output string </returns>
Public static string UBBToHTML (string sDetail)
{
Regex r;
Match m;
# Region processing space
SDetail = sDetail. Replace ("","");
# Endregion
# Region html Tag
SDetail = sDetail. Replace ("<", "<");
SDetail = sDetail. Replace (">", "> ");
# Endregion
# MARK [B] [/B] At region
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 (), "<B>" + m. groups [2]. toString () + "</B> ");
}
# Endregion
# [I] [/I] Mark at region
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 (), "<I>" + m. groups [2]. toString () + "</I> ");
}
# Endregion
# [U] [/u] Mark at region
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 (), "<U>" + m. groups [2]. toString () + "</U> ");
}
# Endregion
# [P] [/p] Mark at region
// Mark [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
# [Sup] [/sup] Mark at region
// Mark [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 (), "<SUP>" + m. groups [2]. toString () + "</SUP> ");
}
# Endregion
# [Sub] [/sub] Mark at region
// Mark [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 (), "<SUB>" + m. groups [2]. toString () + "</SUB> ");
}
# Endregion
# [Url] [/url] Mark at region
// Mark [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 (),
"<A href = \" "+ m. groups [2]. toString () + "\" target = \ "_ blank \"> "+
M. Groups [2]. ToString () + "</A> ");
}
# Endregion
# [Url = xxx] [/url] Mark at region
// Mark [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 (),
"<A href = \" "+ m. groups [2]. toString () + "\" target = \ "_ blank \"> "+
M. Groups [3]. ToString () + "</A> ");
}
# Endregion
# [Email] [/email] Mark at region
// Mark [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 (),
"<A href = \" mailto: "+ m. groups [2]. toString () + "\" target = \ "_ blank \"> "+
M. Groups [2]. ToString () + "</A> ");
}
# Endregion
# [Email = xxx] [/email] Mark at region
// Mark [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 (),
"<A href = \" mailto: "+ m. groups [2]. toString () + "\" target = \ "_ blank \"> "+
M. Groups [3]. ToString () + "</A> ");
}
# Endregion
# [Size = x] [/size] Mark at region
// Mark [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 (),
"<Font size =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</FONT> ");
}
# Endregion
# [Color = x] [/color] Mark at region
// Mark [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 (),
"<Font color =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</FONT> ");
}
# Endregion
# [Font = x] [/font] Mark at region
// Mark [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 (),
"<Font face =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</FONT> ");
}
# Endregion
# Region process image links
// Process image links
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 (),
"<A href = \" ShowImage. aspx? Type = ALL & Action = forumImage & ImageID = "+ m. Groups [1]. ToString () +
"\" Target = \ "_ blank \"> <IMG border = 0 Title = \ "click to open a new window to view \" src = \ "ShowImage. aspx? Action = forumImage & ImageID = "+ m. Groups [1]. ToString () +
"\"> </A> ");
}
# Endregion
# Region processing [align = x] [/align]
// Process [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 (),
"<P align =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</P> ");
}
# Endregion
# [H = x] [/H] Mark at region
// Mark [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 (),
"<H" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</H" + m. Groups [2]. ToString () + "> ");
}
# Endregion
# Region processing [list = x] [*] [/list]
// Process [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 (), "<LI>" + mLI. Groups [1]);
}
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<Ul type = \" "+ m. groups [3]. toString () + "\"> <B> "+ m. groups [4]. toString () + "</B>" +
StrLI + "</UL> ");
}
# Endregion
# Region process line feed
// Process line breaks and add two spaces at the beginning of each new line
R = new Regex (@ "(\ r \ n () |) + )(? <Body> \ S +) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (), "<BR>" + m. Groups ["body"]. ToString ());
}
// Process line breaks and add two spaces at the beginning of each new line
SDetail = sDetail. Replace ("\ r \ n", "<BR> ");
# Endregion
Return sDetail;
}
# Endregion
}
}
Asp.net (c #) ubb processing class
Http://www.jb51.net/article/15615.htm