亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Regex > 正文

C#正則表達式實現Ubb解析類的代碼

2020-03-16 21:21:22
字體:
來源:轉載
供稿:網友
解析得到的代碼能通過XHTML 1.0 STRICT驗證; 
包含了標題,鏈接,字體,對齊,圖片,引用,列表等方面的功能.  
Ubb.ReadMe.htm
  1.     <div id="ubb">   
  2.       <h1>UBB代碼說明</h1>   
  3.  
  4.       <h2>標題</h2>   
  5.       <dl>   
  6.         <dt>[h1]標題一[/h1]</dt>   
  7.         <dd><h1>標題一</h1></dd>   
  8.         <dt>[h2]標題二[/h2]</dt>   
  9.  
  10.         <dd><h2>標題二</h2></dd>   
  11.         <dt>[h1]標題三[/h1]</dt>   
  12.         <dd><h3>標題三</h3></dd>   
  13.         <dt>[h4]標題四[/h4]</dt>   
  14.         <dd><h4>標題四</h4></dd>   
  15.         <dt>[h5]標題五[/h5]</dt>   
  16.  
  17.         <dd><h5>標題五</h5></dd>   
  18.         <dt>[h6]標題六[/h6]</dt>   
  19.         <dd><h6>標題六</h6></dd>   
  20.       </dl>   
  21.       <h2>鏈接</h2>   
  22.       <dl>   
  23.         <dt>[url]www.unibetter.com[/url]</dt>   
  24.  
  25.         <dd><a rel="nofollow" href="http://www.unibetter.com">unibetter.com</a></dd>   
  26.         <dt>[url]http://www.unibetter.com[/url]</dt>   
  27.         <dd><a rel="nofollow" href="http://www.unibetter.com">http://www.unibetter.com</a></dd>   
  28.         <dt>[url=http://www.unibetter.com]unibetter大學生論壇[/url]</dt>   
  29.         <dd><a rel="nofollow" href="http://www.unibetter.com" title="unibetter大學生論壇">unibetter大學生論壇</a></dd>   
  30.         <dt>[email]none@none.com[/email]</dt>   
  31.  
  32.         <dd><a href="mailto:none@none.com">none@none.com</a></dd>   
  33.       </dl>   
  34.       <h2>字體大小</h2>   
  35.       <dl>   
  36.         <dt>[size=xx-small]文字大小[/size]</dt>   
  37.         <dd><span style="font-size:xx-small">文字大小</span></dd>   
  38.         <dt>[size=x-small]文字大小[/size]</dt>   
  39.  
  40.         <dd><span style="font-size:x-small">文字大小</span></dd>   
  41.         <dt>[size=small]文字大小[/size]</dt>   
  42.         <dd><span style="font-size:small">文字大小</span></dd>   
  43.         <dt>[size=medium]文字大小[/size]</dt>   
  44.         <dd><span style="font-size:medium">文字大小</span></dd>   
  45.         <dt>[size=large]文字大小[/size]</dt>   
  46.  
  47.         <dd><span style="font-size:large">文字大小</span></dd>   
  48.         <dt>[size=x-large]文字大小[/size]</dt>   
  49.         <dd><span style="font-size:x-large">文字大小</span></dd>   
  50.         <dt>[size=xx-large]文字大小[/size]</dt>   
  51.         <dd><span style="font-size:xx-large">文字大小</span></dd>   
  52.       </dl>   
  53.  
  54.       <h2>字體格式</h2>   
  55.       <dl>   
  56.         <dt>[font=黑體]文字字體[/font]</dt>   
  57.         <dd><span style="font-family:黑體">文字字體</span></dd>   
  58.         <dt>[color=red]文字顏色[/color]</dt>   
  59.         <dd><span style="color:red">文字顏色</span></dd>   
  60.  
  61.         <dt>[color=#0000ff]文字顏色[/color]</dt>   
  62.         <dd><span style="color:#0000ff">文字顏色</span></dd>   
  63.         <dt>[b]粗體字[/b]</dt>   
  64.         <dd><strong>粗體字</strong></dd>   
  65.         <dt>[i]斜體字[/i]</dt>   
  66.         <dd><i>斜體字</i></dd>   
  67.  
  68.         <dt>[u]下劃線[/u]</dt>   
  69.         <dd><span style="text-decoration:underline">下劃線</span></dd>   
  70.       </dl>   
  71.     </div>   
  72.     <h2>對齊</h2>   
  73.     <dl>   
  74.         <dt>[left]左對齊[/left]</dt>   
  75.  
  76.         <dd><div style="text-align:left">左對齊</div></dd>   
  77.         <dt>[right]右對齊[/right]</dt>   
  78.         <dd><div style="text-align:right">右對齊</div></dd>   
  79.         <dt>[center]右對齊[/center]</dt>   
  80.         <dd><div style="text-align:center">居中</div></dd>   
  81.     </dl>   
  82.  
  83.     <h2>圖片</h2>   
  84.     <dl>   
  85.         <dt>[hr]</dt>   
  86.         <dd><hr /></dd>   
  87.         <dt>[img]http://www.w3.org/Icons/valid-xhtml10[/img]</dt>   
  88.         <dd><img src="http://www.w3.org/Icons/valid-xhtml10" alt="" /></dd>   
  89.         <dt>[img=176x62]http://www.w3.org/Icons/valid-xhtml10[/img]</dt>   
  90.  
  91.         <dd><img src="http://www.w3.org/Icons/valid-xhtml10" style="width:176px;height:62px" alt="" /></dd>   
  92.     </dl>   
  93.     <h2>引用</h2>   
  94.     <dl>   
  95.       <dt>[quote]毛主席他老人家說:一切反動派都是紙老虎![/quote]</dt>   
  96.       <dd><blockquote><div>毛主席他老人家說:一切反動派都是紙老虎!</div></blockquote></dd>   
  97.     </dl>   
  98.  
  99.     <h2>列表</h2>   
  100.     <h3>無序列表</h3>   
  101.     <p>[list] 
  102. [*]蘋果 
  103. [*]香蕉 
  104. [*]桃 
  105. [/list]</p>   
  106.     <ul><li>蘋果</li><li>香蕉</li><li></li></ul>   
  107.  
  108.     <h3>有序列表</h3>   
  109.     <p>[list=1
  110. [*]打開冰箱門 
  111. [*]把大象趕進冰箱 
  112. [*]關上冰箱門 
  113. [/list]</p>   
  114.     <ol style="list-style:decimal;"><li>打開冰箱門</li><li>把大象趕進冰箱</li><li>關上冰箱門</li></ol>   
  115.     <p>[list=i] 
  116. [*]打開冰箱門 
  117. [*]把大象趕進冰箱 
  118. [*]關上冰箱門 
  119. [/list]</p>   
  120.  
  121.     <ol style="list-style:lower-roman;"><li>打開冰箱門</li><li>把大象趕進冰箱</li><li>關上冰箱門</li></ol>   
  122.     <p>[list=I
  123. [*]打開冰箱門 
  124. [*]把大象趕進冰箱 
  125. [*]關上冰箱門 
  126. [/list]</p>   
  127.     <ol style="list-style:upper-roman;"><li>打開冰箱門</li><li>把大象趕進冰箱</li><li>關上冰箱門</li></ol>   
  128.  
  129.     <p>[list=a
  130. [*]打開冰箱門 
  131. [*]把大象趕進冰箱 
  132. [*]關上冰箱門 
  133. [/list]</p>   
  134.     <ol style="list-style:lower-alpha;"><li>打開冰箱門</li><li>把大象趕進冰箱</li><li>關上冰箱門</li></ol>   
  135.     <p>[list=A
  136. [*]打開冰箱門 
  137. [*]把大象趕進冰箱 
  138. [*]關上冰箱門 
  139. [/list]</p>   
  140.  
  141.     <ol style="list-style:upper-alpha;"><li>打開冰箱門</li><li>把大象趕進冰箱</li><li>關上冰箱門</li></ol>   
  142. </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;  
        }  
    }  
 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲一区国产精品| 亚州欧美日韩中文视频| 欧美人与性动交a欧美精品| 日韩精品在线电影| 国产福利成人在线| 亚洲第一精品久久忘忧草社区| www.日本久久久久com.| 久久福利视频网| 欧美日韩在线观看视频小说| 精品日本高清在线播放| 欧美一级淫片aaaaaaa视频| 91午夜理伦私人影院| 欧美精品激情视频| 免费不卡欧美自拍视频| 国内精品久久久久久久| 国产精品久久久久久一区二区| 91精品视频专区| 另类色图亚洲色图| 欧美日韩国产专区| 欧美黑人性猛交| 成人精品一区二区三区电影免费| 久久婷婷国产麻豆91天堂| 亚洲四色影视在线观看| 欧美精品制服第一页| 精品日韩美女的视频高清| 亚洲人成在线观看| 亚洲国产美女久久久久| 国产成人亚洲综合青青| 欧美日韩激情美女| 综合136福利视频在线| 黑人巨大精品欧美一区二区一视频| 国产最新精品视频| 亚洲精品久久久久久久久久久久| 亚洲www视频| 国产伦精品一区二区三区精品视频| 精品一区精品二区| 欧美风情在线观看| 日韩av最新在线观看| 久久免费国产精品1| 国产在线观看精品一区二区三区| 亚洲sss综合天堂久久| 亚洲国产高清高潮精品美女| 久久精品成人欧美大片古装| 中文字幕亚洲二区| 一区二区三区国产视频| 操人视频在线观看欧美| 午夜精品蜜臀一区二区三区免费| 永久555www成人免费| xvideos亚洲人网站| 亚洲欧美日韩天堂| 日韩中文在线不卡| 97人人模人人爽人人喊中文字| 国产成人涩涩涩视频在线观看| 精品久久久久国产| 亚洲专区国产精品| 97在线视频免费看| 日韩欧美一区二区在线| 国产精品视频网| 国产欧美一区二区三区在线看| 欧美电影院免费观看| 在线播放精品一区二区三区| 久热爱精品视频线路一| 97在线看福利| 国产99久久精品一区二区 夜夜躁日日躁| 欧美香蕉大胸在线视频观看| 久久91精品国产91久久久| 日韩精品极品在线观看播放免费视频| 国产成人精品免费久久久久| 精品国产一区二区三区久久| www.欧美免费| 久久久久久噜噜噜久久久精品| 欧美另类69精品久久久久9999| 久久久综合免费视频| 性欧美长视频免费观看不卡| 国产免费一区视频观看免费| 神马久久桃色视频| 午夜精品久久久久久久99热| 久久全球大尺度高清视频| 狠狠躁夜夜躁人人躁婷婷91| 中文字幕亚洲综合| 2021国产精品视频| 久久久午夜视频| 欧美性猛交xxxx免费看久久久| 日韩一区av在线| 精品视频偷偷看在线观看| 68精品国产免费久久久久久婷婷| 欧美精品在线看| 欧美成人小视频| 免费97视频在线精品国自产拍| 日韩av最新在线| 久久久久久久久网站| 色偷偷888欧美精品久久久| 亚洲人成电影在线播放| 在线成人激情视频| 亚洲色在线视频| 一区二区三区 在线观看视| 国产免费一区视频观看免费| 国语自产偷拍精品视频偷| 国产精品美女www爽爽爽视频| 久久91亚洲人成电影网站| 日韩欧美在线字幕| 日韩成人黄色av| 亚洲一区二区三区在线免费观看| 97**国产露脸精品国产| 亚洲毛茸茸少妇高潮呻吟| 中文字幕自拍vr一区二区三区| 国产精品免费一区二区三区都可以| 亚洲色图美腿丝袜| 欧美极品少妇xxxxⅹ裸体艺术| 色偷偷av亚洲男人的天堂| 欧美性生交xxxxx久久久| 久久久国产一区| 欧美人与物videos| 欧美日本啪啪无遮挡网站| 欧美成人亚洲成人日韩成人| 日韩精品www| 欧美激情在线播放| 日韩精品一区二区视频| 成人久久一区二区三区| 亚洲国产欧美久久| 91香蕉亚洲精品| 欧美日韩高清区| 亚洲国产一区自拍| 亚洲偷欧美偷国内偷| 欧美老少配视频| 国产精品久久久久久久久男| 亚洲影院色无极综合| 日韩电影中文 亚洲精品乱码| 欧美日韩免费观看中文| 国产精品久久久久久久久久久久久| 亚洲欧美精品伊人久久| 主播福利视频一区| 大胆欧美人体视频| 精品国产自在精品国产浪潮| 国产精品综合不卡av| 色一情一乱一区二区| 亚洲男女性事视频| 色视频www在线播放国产成人| 91久久久久久久| 国产欧美日韩精品丝袜高跟鞋| 日韩欧美精品中文字幕| 国产精品夜色7777狼人| 亚洲精品视频在线播放| 中文字幕国产精品| 97香蕉久久夜色精品国产| 日韩亚洲第一页| 欧美日韩在线视频一区二区| 久久精品免费电影| 欧美成人午夜激情在线| 色妞一区二区三区| 久久中国妇女中文字幕| 亚洲欧洲黄色网| 国产一区二区三区丝袜| 日本精品久久电影| 国产精国产精品| 欧美日韩国产精品一区二区不卡中文| 欧美人与性动交| 亚洲91精品在线观看| 国产99久久精品一区二区 夜夜躁日日躁| 欧美在线视频网站| 日韩成人中文字幕在线观看| 这里只有精品视频在线| 亚洲欧美国内爽妇网| 久久久国产视频91|