国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - 正則表達式 - C#正則實現(xiàn)Ubb解析類的代碼

C#正則實現(xiàn)Ubb解析類的代碼

2020-07-16 15:59正則表達式教程網(wǎng) 正則表達式

本文主要講解使用C#正則實現(xiàn)Ubb解析類的代碼,有需要的朋友可以參考下

解析得到的代碼能通過XHTML 1.0 STRICT驗證;
包含了標題,鏈接,字體,對齊,圖片,引用,列表等方面的功能. 
Ubb.ReadMe.htm


 

復(fù)制代碼 代碼如下:


//作者: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; 
        } 
    } 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产精品极品美女在线观看免费 | 欧美日韩中文在线观看 | 国产一区中文字幕 | 精品福利一区二区三区 | 97久久香蕉国产线看观看 | 成人中文字幕在线观看 | 久久h| 成人深夜在线观看 | 日韩成人片 | 欧美一区二区三区在线看 | 99re在线播放视频 | 午夜精品一区二区三区在线视频 | 另类久久 | 日韩成人中文字幕 | 欧美日韩免费视频 | av中文字幕在线观看 | 亚洲综合视频在线观看 | 在线观看亚洲a | 特黄一级 | 含羞草www国产在线视频 | 国产成人99久久亚洲综合精品 | 欧美一区二区三区免费视频 | 国产精品久久久久无码av | 天堂中文网官网 | 亚洲一区二区在线 | 久久精品一区二区三区四区 | 日韩在线播放一区二区 | 黄色三级网站 | 五月天狠狠爱 | 激情中文网 | 国产精品久久久久久久久久久久久久 | 欧美亚洲在线 | 亚洲专区中文字幕 | 丁香婷婷综合激情五月色 | 久久h | 亚洲国产精品人人爽夜夜爽 | 波多野结衣先锋影音 | www.嫩草| 亚洲欧美观看 | 成人理论片| 成人a级片在线观看 |