麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > 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][email protected][/email]</dt>   
  31.  
  32.         <dd><a href="mailto:[email protected]">[email protected]</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;  
        }  
    }  
 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 色中色激情影院 | 毛片在哪里看 | 久久久aa | 国产午夜精品一区二区三区不卡 | 在线成人免费观看 | 欧美成人免费电影 | 国产成人精品午夜视频' | 视频久久免费 | 免费黄色短视频网站 | 国产毛片在线看 | 中文字幕电影免费播放 | 在线播放91 | 欧美在线a | 欧美激情精品久久久久久黑人 | 泰剧19禁啪啪无遮挡大尺度 | 国内精品视频饥渴少妇在线播放 | 色视频在线播放 | 亚洲欧美日韩精品久久 | av免费在线免费观看 | 国产一级毛片国语版 | 欧美中文字幕在线视频 | 欧美亚洲一级 | 一级毛片电影院 | 日韩黄在线 | 日产精品一区二区三区在线观看 | 91精品国产91久久久久久蜜臀 | xxxx hd video 69| 日韩毛片免费观看 | 好吊色欧美一区二区三区四区 | 天堂精品久久 | 欧美a视频在线观看 | 奶子吧naiziba.cc免费午夜片在线观看 | 欧美一级在线免费 | 国产精品视频导航 | 欧美日韩手机在线观看 | 日本在线观看中文字幕 | 亚洲网视频 | 高清国产午夜精品久久久久久 | 99热久草 | 一本到免费视频 | 一区二区三区四区高清视频 |