摘自,CSDN:标准的UBB正则表达式。

http://expert.csdn.net/Expert/topic/2889/2889317.xml?temp=.2898523

http://search.csdn.net/expert/topic/52/5202/2003/1/12/1351125.xml

啥叫“标准”的,我不是很理解,:(- guoshuang

转帖:

public static string FormatDetail(string sDetail)

{

Regex r;

Match m;

//处理空格

sDetail = sDetail.Replace(” “,” ”);

sDetail = sDetail.Replace(”<","<");

sDetail = sDetail.Replace(">“,”>”);

//sDetail = sDetail.Replace(”
“,”

“);

//sDetail = sDetail.Replace(”
“,”
“);

//处标记

r = new Regex(@”([b])([ S ]*?)([/b])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),”” + m.Groups[2].ToString() + ““);

}

//处标记

r = new Regex(@”([i])([ S ]*?)([/i])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),”” + m.Groups[2].ToString() + ““);

}

//处标记

r = new Regex(@”([U])([ S ]*?)([/U])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),”” + m.Groups[2].ToString() + ““);

}

//处[p][/p]标记

r = new Regex(@”((
)*[p])(.*?)((
)*[/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() + “

“);

}

//处[sup][/sup]标记

r = new Regex(@”([sup])([ S ]*?)([/sup])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),”” + m.Groups[2].ToString() + ““);

}

//处[sub][/sub]标记

r = new Regex(@”([sub])([ S ]*?)([/sub])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),”” + m.Groups[2].ToString() + ““);

}

//处[url][/url]标记

r = new Regex(@”([url])([ S ]*?)([/url])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[2].ToString() + ““);

}

//处[url=xxx][/url]标记

r = new Regex(@”([url=([ S ]+)])([ S ]*?)([/url])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[3].ToString() + ““);

}

//处[email][/email]标记

r = new Regex(@”([email])([ S ]*?)([/email])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[2].ToString() + ““);

}

//处[email=xxx][/email]标记

r = new Regex(@”([email=([ S ]+)])([ S ]*?)([/email])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[3].ToString() + ““);

}

//处[size=x][/size]标记

r = new Regex(@”([size=([1-7])])([ S ]*?)([/size])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[3].ToString() + ““);

}

//处[color=x][/color]标记

r = new Regex(@”([color=([S]+)])([ S ]*?)([/color])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[3].ToString() + ““);

}

//处[font=x][/font]标记

r = new Regex(@”([font=([S]+)])([ S ]*?)([/font])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[3].ToString() + ““);

}

//处理图片链接

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">

"">“);

}

//处理[align=x][/align]

r = new Regex(@”([align=([S]+)])([ S ]*?)([/align])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

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

“);

}

//处[H=x][/H]标记

r = new Regex(@”([H=([1-6])])([ S ]*?)([/H])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

sDetail = sDetail.Replace(m.Groups[0].ToString(),

” +

m.Groups[3].ToString() + ““);

}

//处理[list=x][*][/list]

r = new Regex(@”([list(=(A|a|I|i| ))?]([ S ]*)
)(([*]([ S ]*
))*?)([/list])”,RegexOptions.IgnoreCase);

for (m = r.Match(sDetail); m.Success; m = m.NextMatch())

{

string strLI = m.Groups[5].ToString();

Regex rLI = new Regex(@”[*]([ S ]*
?)”,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 + “

    “);

    }

    //处理换行,在每个新行的前面添加两个全角空格

    r = new Regex(@”(
    (( )| )+)(?<正文>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(”
    “,”
    “);

    return sDetail;

    //return “

    ” + sDetail + “

    “;

    }

    [align=right][size=1][color=#cccccc][Edit on 2004-9-13 19:12:13 By guoshuang][/color][/size][/align]