C#正则实现Ubb解析类的代码
作者:mdxy-dxy 发布时间:2021-10-23 22:19:38
标签:C#正则实现Ubb解析类的代码
解析得到的代码能通过XHTML 1.0 STRICT验证;
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
Ubb.ReadMe.htm
<div id="ubb"> <h1>UBB代码说明</h1> <h2>标题</h2> <dl> <dt>[h1]标题一[/h1]</dt> <dd><h1>标题一</h1></dd> <dt>[h2]标题二[/h2]</dt> <dd><h2>标题二</h2></dd> <dt>[h1]标题三[/h1]</dt> <dd><h3>标题三</h3></dd> <dt>[h4]标题四[/h4]</dt> <dd><h4>标题四</h4></dd> <dt>[h5]标题五[/h5]</dt> <dd><h5>标题五</h5></dd> <dt>[h6]标题六[/h6]</dt> <dd><h6>标题六</h6></dd> </dl> <h2>链接</h2> <dl> <dt>[url]www.unibetter.com[/url]</dt> <dd>unibetter.com</dd> <dt>[url]http://www.unibetter.com[/url]</dt> <dd>http://www.unibetter.com</dd> <dt>[url=http://www.unibetter.com]unibetter大学生论坛[/url]</dt> <dd>unibetter大学生论坛</dd> <dt>[email]none@none.com[/email]</dt> <dd>none@none.com</dd> </dl> <h2>字体大小</h2> <dl> <dt>[size=xx-small]文字大小[/size]</dt> <dd>文字大小</dd> <dt>[size=x-small]文字大小[/size]</dt> <dd>文字大小</dd> <dt>[size=small]文字大小[/size]</dt> <dd>文字大小</dd> <dt>[size=medium]文字大小[/size]</dt> <dd>文字大小</dd> <dt>[size=large]文字大小[/size]</dt> <dd>文字大小</dd> <dt>[size=x-large]文字大小[/size]</dt> <dd>文字大小</dd> <dt>[size=xx-large]文字大小[/size]</dt> <dd>文字大小</dd> </dl> <h2>字体格式</h2> <dl> <dt>[font=黑体]文字字体[/font]</dt> <dd>文字字体</dd> <dt>[color=red]文字颜色[/color]</dt> <dd>文字颜色</dd> <dt>[color=#0000ff]文字颜色[/color]</dt> <dd>文字颜色</dd> <dt>[b]粗体字[/b]</dt> <dd><strong>粗体字</strong></dd> <dt>[i]斜体字[/i]</dt> <dd><i>斜体字</i></dd> <dt>[u]下划线[/u]</dt> <dd>下划线</dd> </dl> </div> <h2>对齐</h2> <dl> <dt>[left]左对齐[/left]</dt> <dd><div style="text-align:left">左对齐</div></dd> <dt>[right]右对齐[/right]</dt> <dd><div style="text-align:right">右对齐</div></dd> <dt>[center]右对齐[/center]</dt> <dd><div style="text-align:center">居中</div></dd> </dl> <h2>图片</h2> <dl> <dt>[hr]</dt> <dd><hr /></dd> <dt>[img]http://www.w3.org/Icons/valid-xhtml10[/img]</dt> <dd><img src="http://www.w3.org/Icons/valid-xhtml10" alt="" /></dd> <dt>[img=176x62]http://www.w3.org/Icons/valid-xhtml10[/img]</dt> <dd><img src="http://www.w3.org/Icons/valid-xhtml10" style="width:176px;height:62px" alt="" /></dd> </dl> <h2>引用</h2> <dl> <dt>[quote]纸老虎![/quote]</dt> <dd><blockquote><div>纸老虎!</div></blockquote></dd> </dl> <h2>列表</h2> <h3>无序列表</h3> <p>[list][*]苹果[*]香蕉[*]桃[/list]</p> <ul><li>苹果</li><li>香蕉</li><li>桃</li></ul> <h3>有序列表</h3> <p>[list=1][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]</p> <ol style="list-style:decimal;"><li>打开冰箱门</li><li>把大象赶进冰箱</li><li>关上冰箱门</li></ol> <p>[list=i][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]</p> <ol style="list-style:lower-roman;"><li>打开冰箱门</li><li>把大象赶进冰箱</li><li>关上冰箱门</li></ol> <p>[list=I][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]</p> <ol style="list-style:upper-roman;"><li>打开冰箱门</li><li>把大象赶进冰箱</li><li>关上冰箱门</li></ol> <p>[list=a][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]</p> <ol style="list-style:lower-alpha;"><li>打开冰箱门</li><li>把大象赶进冰箱</li><li>关上冰箱门</li></ol> <p>[list=A][*]打开冰箱门[*]把大象赶进冰箱[*]关上冰箱门[/list]</p> <ol style="list-style:upper-alpha;"><li>打开冰箱门</li><li>把大象赶进冰箱</li><li>关上冰箱门</li></ol> </div>
//作者: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;
/// <summary>
/// 解析Ubb代码为Html代码
/// </summary>
/// <param name="ubb">Ubb代码</param>
/// <returns>解析得到的Html代码</returns>
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;
}
/// <summary>
/// 解析Ubb代码为Html代码,所有的链接为rel="nofollow"
/// </summary>
/// <param name="ubb">Ubb代码</param>
/// <returns>解析得到的Html代码</returns>
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)\](.*?)\[/h\1\]", "<h$1>$2</h$1>", options);
return result;
}
private static string DecodeList(string ubb)
{
string sListFormat = "<ol style=\"list-style:{0};\">$1</ol>";
string result = 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);
return result;
}
private static string DecodeBlank(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"(?<= ) | (?= )", " ", options);
result = Regex.Replace(result, @"\r\n", "<br />");
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("<br />(<" + tag + ")",options);
result = r.Replace(result, "$1");
r = new Regex("<br />(</" + tag + ")",options);
result = r.Replace(result, "$1");
}
return result;
}
private static string DecodeAlign(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[left\](.*?)\[/left\]", "<div style=\"text-align:left\">$1</div>", options);
result = Regex.Replace(result, @"\[right\](.*?)\[/right\]", "<div style=\"text-align:right\">$1</div>", options);
result = Regex.Replace(result, @"\[center\](.*?)\[/center\]", "<div style=\"text-align:center\">$1</div>", options);
return result;
}
private static string DecodeQuote(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[quote\]", "<blockquote><div>", options);
result = Regex.Replace(result, @"\[/quote\]", "</div></blockquote>", options);
return result;
}
private static string DecodeFont(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[size=([-\w]+)\](.*?)\[/size\]", "<span style=\"font-size:$1\">$2</span>", options);
result = Regex.Replace(result, @"\[font=(.*?)\](.*?)\[/font\]", "<span style=\"font-family:$1\">$2</span>", options);
return result;
}
private static string DecodeLinks(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a href=\"http://www.$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a href=\"$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a href=\"$1\" title=\"$2\">$2</a>", options);
result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options);
return result;
}
private static string DecodeLinksNoFollow(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a rel=\"nofollow\" href=\"http://www.$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\" title=\"$2\">$2</a>", options);
result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options);
return result;
}
private static string DecodeImage(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[hr\]", "<hr />", options);
result = Regex.Replace(result, @"\[img\](.+?)\[/img\]", "<img src=\"$1\" alt=\"\" />", options);
result = Regex.Replace(result, @"\[img=(\d+)x(\d+)\](.+?)\[/img\]", "<img src=\"$3\" style=\"width:$1px;height:$2px\" alt=\"\" />", options);
return result;
}
private static string DecodeColor(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[color=(#?\w+?)\](.+?)\[/color\]", "<span style=\"color:$1\">$2</span>",options);
return result;
}
private static string DecodeStyle(string ubb)
{
string result=ubb;
//we don’t need this for perfomance and other consideration:
//(<table[^>]*>(?><table[^>]*>(?<Depth>)|</table>(?<-Depth>)|.)+(?(Depth)(?!))</table>)
result = Regex.Replace(result, @"\[[b]\](.*?)\[/[b]\]", "<strong>$1</strong>", options);
result = Regex.Replace(result, @"\[[u]\](.*?)\[/[u]\]", "<span style=\"text-decoration:underline\">$1</span>", options);
result = Regex.Replace(result, @"\[[i]\](.*?)\[/[i]\]", "<i>$1</i>", options);
return result;
}
}
}


猜你喜欢
- 回文数判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。示例 1:输入: 121输出: true示例&
- 本文将介绍使用Spring Boot集成Mybatis并实现主从库分离的实现(同样适用于多数据源)。延续之前的Spring Boot 集成M
- java中next与nextLine用法区别:next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或E
- 单元测试是程序员对代码的自测,一般公司都会严格要求单元测试,这是对自己代码的负责,也是对代码的敬畏。一般单元测试都是测试Service层,下
- android多线程断点下载,带进度条和百分比显示,断点下载的临时数据保存到SD卡的文本文档中,建议可以保存到本地数据库中,这样可以提高存取
- 本文介绍 Spring Boot 项目中整合 ElasticSearch 并实现 CRUD 操作,包括分页、滚动等功能。之前在公司使用 ES
- 本文实例讲解了统计文本中某个字符串出现的次数或字符串中指定元素出现的次数方法,分享给大家供大家参考,具体内容如下运行效果图:程序查找的上此文
- 同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++/python/Java,结合
- 泛型概述我们都知道集合中是可以存放任意对象的,只要把对象存储集合后,那么这时他们都会被提升成Object类型。当我们在取出每一个对象,并且进
- 本文实例为大家分享了Unity苹果手机Taptic震动的具体代码,供大家参考,具体内容如下文件:ios震动.zip将上方文件解压之后将Mul
- 1、long long 和 __int64在C++ Primer当中提到的64位的int只有long long,但是在实际各种各样的C++编
- /// <summary> /// 计算本周起始日期(礼拜一的日期) /// </summary&
- 上一篇已经获取到了用户的OpenId这篇主要是调用微信公众支付的统一下单APIAPI地址:https://pay.weixin.qq.com
- 为了能正常输出XML格式的内容,必须要对不被XML允许的那些特殊字符进行转换。本文介绍的正是如何使用C#判断XML字符串是否含特殊字符并进行
- 由于最近想要阅读下JDK1.8 中HashMap的具体实现,但是由于HashMap的实现中用到了红黑树,所以我觉得有必要先复习下红黑树的相关
- Semaphore、SemaphoreSlim 类两者都可以限制同时访问某一资源或资源池的线程数。这里先不扯理论,我们从案例入手,通过示例代
- 上下文:程序运行需要的环境(外部变量)上下文切换:将之前的程序需要的外部变量复制保存,然后切换到新的程序运行环境系统调用:(用户态陷入操作系
- 概述最近项目上反馈某个重要的定时任务突然不执行了,很头疼,开发环境和测试环境都没有出现过这个问题。定时任务采用的是ScheduledThre
- 1. Mybatis分页插件1.1 分页插件介绍分页可以将很多条结果进行分页显示。如果当前在第一页,则没有上一页。如果当前在最后一页,则没有
- Java 8来了,是时候学一下新的东西了。Java 7和Java 6只不过是稍作修改的版本,而Java 8将会发生重大的改进。或许是Java