C#正则实现Ubb解析类的代码
//作者:deerchao
// http://www.unibetter.com/blogs/blogdeerchao/default.aspx
//在不移除以上(及本条)注释的前提下,任何人可以以任何方式使用此代码.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace Deerchao.Web
{
public class UbbDecoder
{
private static readonly RegexOptions options = RegexOptions.Compiled | RegexOptions.Singleline;
///
/// 解析Ubb代码为Html代码
///
/// Ubb代码
///
public static string Decode(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return null;
string result = ubb;
result = HttpUtility.HtmlEncode(result);
result = DecodeStyle(result);
result = DecodeFont(result);
result = DecodeColor(result);
result = DecodeImage(result);
result = DecodeLinks(result);
result = DecodeQuote(result);
result = DecodeAlign(result);
result = DecodeList(result);
result = DecodeHeading(result);
result = DecodeBlank(result);
return result;
}
///
/// 解析Ubb代码为Html代码,所有的链接为rel="nofollow"
///
/// Ubb代码
///
public static string DecodeNoFollow(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return null;
string result = ubb;
result = HttpUtility.HtmlEncode(result);
result = DecodeStyle(result);
result = DecodeFont(result);
result = DecodeColor(result);
result = DecodeImage(result);
result = DecodeLinksNoFollow(result);
result = DecodeQuote(result);
result = DecodeAlign(result);
result = DecodeList(result);
result = DecodeHeading(result);
result = DecodeBlank(result);
return result;
}
private static string DecodeHeading(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[h(d)](.*?)[/h1]", "
return result;
}
private static string DecodeList(string ubb)
{
string sListFormat = "
- $1
string result = ubb;
// Lists
result = Regex.Replace(result, @"[*]([^[]*)", "
result = Regex.Replace(result, @"[list]s*(.*?)[/list]", "
- $1
result = Regex.Replace(result, @"[list=1]s*(.*?)[/list]", string.Format(sListFormat, "decimal"), options);
result = Regex.Replace(result, @"[list=i]s*(.*?)[/list]", string.Format(sListFormat, "lower-roman"), options);
result = Regex.Replace(result, @"[list=I]s*(.*?)[/list]", string.Format(sListFormat, "upper-roman"), options);
result = Regex.Replace(result, @"[list=a]s*(.*?)[/list]", string.Format(sListFormat, "lower-alpha"), options);
result = Regex.Replace(result, @"[list=A]s*(.*?)[/list]", string.Format(sListFormat, "upper-alpha"), options);
return result;
}
private static string DecodeBlank(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"(?<= ) | (?= )", " ", options);
result = Regex.Replace(result, @"
", "
");
string[] blockTags = {"h[1-6]", "li", "list", "div", "p", "ul"};
//clear br before block tags(start or end)
foreach (string tag in blockTags)
{
Regex r = new Regex("
(<" + tag + ")",options);
result = r.Replace(result, "$1");
r = new Regex("
(" + tag + ")",options);
result = r.Replace(result, "$1");
}
return result;
}
private static string DecodeAlign(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[left](.*?)[/left]", "
result = Regex.Replace(result, @"[right](.*?)[/right]", "
result = Regex.Replace(result, @"[center](.*?)[/center]", "
return result;
}
private static string DecodeQuote(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[quote]", "
", options);", options);result = Regex.Replace(result, @"[/quote]", "
return result;
}
private static string DecodeFont(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[size=([-w]+)](.*?)[/size]", "$2", options);
result = Regex.Replace(result, @"[font=(.*?)](.*?)[/font]", "$2", options);
return result;
}
private static string DecodeLinks(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[url]www.(.*?)[/url]", "$1", options);
result = Regex.Replace(result, @"[url](.*?)[/url]", "$1", options);
result = Regex.Replace(result, @"[url=(.*?)](.*?)[/url]", "$2", options);
result = Regex.Replace(result, @"[email](.*?)[/email]", "$1", options);
return result;
}
private static string DecodeLinksNoFollow(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[url]www.(.*?)[/url]", "$1", options);
result = Regex.Replace(result, @"[url](.*?)[/url]", "$1", options);
result = Regex.Replace(result, @"[url=(.*?)](.*?)[/url]", "$2", options);
result = Regex.Replace(result, @"[email](.*?)[/email]", "$1", options);
return result;
}
private static string DecodeImage(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[hr]", "
", options);
result = Regex.Replace(result, @"[img](.+?)[/img]", "", options);
result = Regex.Replace(result, @"[img=(d+)x(d+)](.+?)[/img]", "", options);
return result;
}
private static string DecodeColor(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"[color=(#?w+?)](.+?)[/color]", "$2",options);
return result;
}
private static string DecodeStyle(string ubb)
{
string result=ubb;
//we don’t need this for perfomance and other consideration:
//(
result = Regex.Replace(result, @"[[b]](.*?)[/[b]]", "$1", options);
result = Regex.Replace(result, @"[[u]](.*?)[/[u]]", "$1", options);
result = Regex.Replace(result, @"[[i]](.*?)[/[i]]", "$1", options);
return result;
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播