手机
当前位置:查字典教程网 >编程开发 >正则表达式 >C#正则实现Ubb解析类的代码
C#正则实现Ubb解析类的代码
摘要:解析得到的代码能通过XHTML1.0STRICT验证;包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.Ubb.ReadMe.ht...

解析得到的代码能通过XHTML1.0STRICT验证;

包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.

Ubb.ReadMe.htm

UBB代码说明 标题 [h1]标题一[/h1] 标题一 [h2]标题二[/h2] 标题二 [h1]标题三[/h1] 标题三 [h4]标题四[/h4] 标题四 [h5]标题五[/h5] 标题五 [h6]标题六[/h6] 标题六 链接 [url]www.unibetter.com[/url] unibetter.com [url]http://www.unibetter.com[/url] http://www.unibetter.com [url=http://www.unibetter.com]unibetter大学生论坛[/url] unibetter大学生论坛 [email]none@none.com[/email] none@none.com 字体大小 [size=xx-small]文字大小[/size] 文字大小 [size=x-small]文字大小[/size] 文字大小 [size=small]文字大小[/size] 文字大小 [size=medium]文字大小[/size] 文字大小 [size=large]文字大小[/size] 文字大小 [size=x-large]文字大小[/size] 文字大小 [size=xx-large]文字大小[/size] 文字大小 字体格式 [font=黑体]文字字体[/font] 文字字体 [color=red]文字颜色[/color] 文字颜色 [color=#0000ff]文字颜色[/color] 文字颜色 [b]粗体字[/b] 粗体字 [i]斜体字[/i] 斜体字 [u]下划线[/u] 下划线 对齐 [left]左对齐[/left] 左对齐 [right]右对齐[/right] 右对齐 [center]右对齐[/center] 居中 图片 [hr] [img]http://www.w3.org/Icons/valid-xhtml10[/img]

C#正则实现Ubb解析类的代码1

[img=176x62]http://www.w3.org/Icons/valid-xhtml10[/img]

C#正则实现Ubb解析类的代码1

引用 [quote]毛主席他老人家说:一切反动派都是纸老虎![/quote] 毛主席他老人家说:一切反动派都是纸老虎! 列表 无序列表

[list][*]苹果[*]香蕉[*]桃[/list]

苹果香蕉桃 有序列表

[list=1][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]

打开冰箱门把大象赶进冰箱关上冰箱门

[list=i][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]

打开冰箱门把大象赶进冰箱关上冰箱门

[list=I][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]

打开冰箱门把大象赶进冰箱关上冰箱门

[list=a][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]

打开冰箱门把大象赶进冰箱关上冰箱门

[list=A][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]

打开冰箱门把大象赶进冰箱关上冰箱门

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

复制代码 代码如下:

//作者:deerchao

//http://www.unibetter.com/blogs/blogdeerchao/default.aspx

//在不移除以上(及本条)注释的前提下,任何人可以以任何方式使用此代码.

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Web;

usingSystem.Text.RegularExpressions;

namespaceDeerchao.Web

{

publicclassUbbDecoder

{

privatestaticreadonlyRegexOptionsoptions=RegexOptions.Compiled|RegexOptions.Singleline;

///<summary>

///解析Ubb代码为Html代码

///</summary>

///<paramname="ubb">Ubb代码</param>

///<returns>解析得到的Html代码</returns>

publicstaticstringDecode(stringubb)

{

if(string.IsNullOrEmpty(ubb))

returnnull;

stringresult=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);

returnresult;

}

///<summary>

///解析Ubb代码为Html代码,所有的链接为rel="nofollow"

///</summary>

///<paramname="ubb">Ubb代码</param>

///<returns>解析得到的Html代码</returns>

publicstaticstringDecodeNoFollow(stringubb)

{

if(string.IsNullOrEmpty(ubb))

returnnull;

stringresult=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);

returnresult;

}

privatestaticstringDecodeHeading(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[h(d)](.*?)[/h1]","<h$1>$2</h$1>",options);

returnresult;

}

privatestaticstringDecodeList(stringubb)

{

stringsListFormat="<olstyle="list-style:{0};">$1</ol>";

stringresult=ubb;

//Lists

result=Regex.Replace(result,@"[*]([^[]*)","<li>$1</li>",options);

result=Regex.Replace(result,@"[list]s*(.*?)[/list]","<ul>$1</ul>",options);

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);

returnresult;

}

privatestaticstringDecodeBlank(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"(?<=)|(?=)","",options);

result=Regex.Replace(result,@"rn","<br/>");

string[]blockTags={"h[1-6]","li","list","div","p","ul"};

//clearbrbeforeblocktags(startorend)

foreach(stringtaginblockTags)

{

Regexr=newRegex("<br/>(<"+tag+")",options);

result=r.Replace(result,"$1");

r=newRegex("<br/>(</"+tag+")",options);

result=r.Replace(result,"$1");

}

returnresult;

}

privatestaticstringDecodeAlign(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[left](.*?)[/left]","<divstyle="text-align:left">$1</div>",options);

result=Regex.Replace(result,@"[right](.*?)[/right]","<divstyle="text-align:right">$1</div>",options);

result=Regex.Replace(result,@"[center](.*?)[/center]","<divstyle="text-align:center">$1</div>",options);

returnresult;

}

privatestaticstringDecodeQuote(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[quote]","<blockquote><div>",options);

result=Regex.Replace(result,@"[/quote]","</div></blockquote>",options);

returnresult;

}

privatestaticstringDecodeFont(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[size=([-w]+)](.*?)[/size]","<spanstyle="font-size:$1">$2</span>",options);

result=Regex.Replace(result,@"[font=(.*?)](.*?)[/font]","<spanstyle="font-family:$1">$2</span>",options);

returnresult;

}

privatestaticstringDecodeLinks(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[url]www.(.*?)[/url]","<ahref="http://www.$1">$1</a>",options);

result=Regex.Replace(result,@"[url](.*?)[/url]","<ahref="$1">$1</a>",options);

result=Regex.Replace(result,@"[url=(.*?)](.*?)[/url]","<ahref="$1"title="$2">$2</a>",options);

result=Regex.Replace(result,@"[email](.*?)[/email]","<ahref="mailto:$1">$1</a>",options);

returnresult;

}

privatestaticstringDecodeLinksNoFollow(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[url]www.(.*?)[/url]","<arel="nofollow"href="http://www.$1">$1</a>",options);

result=Regex.Replace(result,@"[url](.*?)[/url]","<arel="nofollow"href="$1">$1</a>",options);

result=Regex.Replace(result,@"[url=(.*?)](.*?)[/url]","<arel="nofollow"href="$1"title="$2">$2</a>",options);

result=Regex.Replace(result,@"[email](.*?)[/email]","<ahref="mailto:$1">$1</a>",options);

returnresult;

}

privatestaticstringDecodeImage(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[hr]","<hr/>",options);

result=Regex.Replace(result,@"[img](.+?)[/img]","<imgsrc="$1"alt=""/>",options);

result=Regex.Replace(result,@"[img=(d+)x(d+)](.+?)[/img]","<imgsrc="$3"style="width:$1px;height:$2px"alt=""/>",options);

returnresult;

}

privatestaticstringDecodeColor(stringubb)

{

stringresult=ubb;

result=Regex.Replace(result,@"[color=(#?w+?)](.+?)[/color]","<spanstyle="color:$1">$2</span>",options);

returnresult;

}

privatestaticstringDecodeStyle(stringubb)

{

stringresult=ubb;

//wedon'tneedthisforperfomanceandotherconsideration:

//(<table[^>]*>(?><table[^>]*>(?<Depth>)|</table>(?<-Depth>)|.)+(?(Depth)(?!))</table>)

result=Regex.Replace(result,@"[[b]](.*?)[/[b]]","<strong>$1</strong>",options);

result=Regex.Replace(result,@"[[u]](.*?)[/[u]]","<spanstyle="text-decoration:underline">$1</span>",options);

result=Regex.Replace(result,@"[[i]](.*?)[/[i]]","<i>$1</i>",options);

returnresult;

}

}

}

【C#正则实现Ubb解析类的代码】相关文章:

正则表达式查找相似单词的方法

JavaScript正则表达式之后向引用实例代码

一个正则表达式的看法(?:)

PHP 正则 email语句详解

JS使用正则去除字符串最后的逗号

使用Linux正则表达式灵活搜索文件中的文本

正则表达式详述 三

asp.net(c#) 使用Rex正则来生成字符串数组的代码

ASP 正则表达式的应用使用说明

正则表达式的使用 ASP

精品推荐
分类导航