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

所属分类: 网络编程 / 正则表达式 阅读数: 580
收藏 0 赞 0 分享

解析得到的代码能通过XHTML 1.0 STRICT验证;
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能. 
Ubb.ReadMe.htm


[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

复制代码 代码如下:

//作者: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, @"(?<= ) | (?= )", "&nbsp;", 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; 
        } 
    } 

更多精彩内容其他人还在看

正则表达式验证IPV4地址功能实例分析

这篇文章主要介绍了正则表达式验证IPV4地址功能,结合实例形式分析了IPV4地址验证的原理及具体实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

正则表达式教程之前后查找lookaround详解

这篇文章主要介绍了正则表达式教程之前后查找lookaround,结合具体问题分析了向前查找及向后查找功能的实现技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

正则匹配密码只能是数字和字母组合字符串功能【php与js实现】

这篇文章主要介绍了正则匹配密码只能是数字和字母组合字符串功能,涉及针对字符、数字等正则操作相关技巧,并给出了php与js实现示例,需要的朋友可以参考下
收藏 0 赞 0 分享

正则验证不能含有中文的实现方法【jQuery与java实现】

这篇文章主要介绍了正则验证不能含有中文的实现方法,结合jQuery与java两种实现方法分析了针对中文的正则验证操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

JS 密码强度校验的正则表达式(简单且好用)

最近在做一个通行证的项目,在项目中有这样的需求,注册模块中输入密码需要显示密码强度,今天小编给大家分享JS 密码强度校验的正则表达式,简单好用,需要的朋友参考下
收藏 0 赞 0 分享

iOS 正则表达式判断纯数字及匹配11位手机号码的方法

这篇文章主要介绍了iOS 正则表达式判断纯数字及匹配11位手机号码的方法,判断手机号码是否正确的方法很多,我是用正则表达式来完成匹配的,具体方法,大家参考下本文
收藏 0 赞 0 分享

正则表达式(简单易懂篇)

正则表达式是一种可以用于模式匹配和替换的强大工具。这篇文章主要介绍了正则表达式(简单易懂篇),需要的朋友参考下
收藏 0 赞 0 分享

正则表达式实现匹配连续数字的方法

我这两天刚刚学正则表达式。我觉的正则对连续的字符匹配很简单,但是对连续的一段数字匹配就不是很好。正好最近有朋友问了匹配连续数字的正则,就帮忙写了一下,算是当作温习一下吧。下面这篇文章就主要介绍了正则表达式实现匹配连续数字的方法。
收藏 0 赞 0 分享

正则表达式简介及在C++11中的简单使用教程

正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex、regexp、RE、regexps、regexes、regexen。接下来通过本文给大家介绍正则表达式简介及在C++11中的简单使用教程,一起通过本文学习吧
收藏 0 赞 0 分享

正则表达式实现最小匹配功能的方法

这篇文章主要介绍了正则表达式实现最小匹配功能的方法,结合具体实例形式分析了正则表达式最小匹配功能的原理与实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多