国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

asp.net正則模板引擎代碼

2019-11-17 01:39:23
字體:
來源:轉載
供稿:網友

asp.net正則模板引擎代碼

我們申明一個數組

   public static Regex[] r = new Regex[23];

接下來關鍵的正則表達式:

            RegexOptions options = RegexOptions.None;            //嵌套模板標簽(兼容)            r[0] = new Regex(@"<!--{template ((skin=//""([^/[/]/{/}/s]+)//""(?:/s+))?)src=(?://|//"")([^/[/]/{/}/s]+)(?://|//"")(?:/s*)}-->", options);            //模板路徑標簽(新增)            r[1] = new Regex(@"<!--{templateskin((=(?://"")([^/[/]/{/}/s]+)(?://""))?)(?:/s*)}-->", options);            //命名空間標簽            r[2] = new Regex(@"<!--{namespace (?:""?)([/s/S]+?)(?:""?)}-->", options);            //C#代碼標簽            r[3] = new Regex(@"<!--{csharp}-->([/s/S]+?)<!--{/csharp}-->", options);            //loop循環(拋棄)            r[4] = new Regex(@"<!--{loop ((/(([^/[/]/{/}/s]+)/) )?)([^/[/]/{/}/s]+) ([^/[/]/{/}/s]+)}-->", options);            //foreach循環(新增)            r[5] = new Regex(@"<!--{foreach(?:/s*)/(([^/[/]/{/}/s]+) ([^/[/]/{/}/s]+) in ([^/[/]/{/}/s]+)/)(?:/s*)}-->", options);            //for循環(新增)            r[6] = new Regex(@"<!--{for/(([^/(/)/[/]/{/}]+)/)(?:/s*)}-->", options);            //if語句標簽(拋棄)            r[7] = new Regex(@"<!--{if (?:/s*)(([^/s]+)((?:/s*)(/|/||/&/&)(?:/s*)([^/s]+))?)(?:/s*)}-->", options);            r[8] = new Regex(@"<!--{else(( (?:/s*)if (?:/s*)(([^/s]+)((?:/s*)(/|/||/&/&)(?:/s*)([^/s]+))*))?)(?:/s*)}-->", options);            //if語句標簽(新增)            r[9] = new Regex(@"<!--{if/((([^/s]+)((?:/s*)(/|/||/&/&)(?:/s*)([^/s]+))?)/)(?:/s*)}-->", options);            r[10] = new Regex(@"<!--{else(( (?:/s*)if/((([^/s]+)((?:/s*)(/|/||/&/&)(?:/s*)([^/s]+))?))?/))(?:/s*)}-->", options);            //循環與判斷結束標簽(兼容)            r[11] = new Regex(@"<!--{//(?:loop|foreach|for|if)(?:/s*)}-->", options);            //continue標簽            r[12] = new Regex(@"<!--{continue(?:/s*)}-->");            //break標簽            r[13] = new Regex(@"<!--{break(?:/s*)}-->");            //request標簽            r[14] = new Regex(@"(/{request/[([^/[/]/{/}/s]+)/]/})", options);            //截取字符串標簽            r[15] = new Regex(@"(<!--{cutstring/(([^/s]+?),(./d*?)/)}-->)", options);            //url鏈接標簽            r[16] = new Regex(@"(<!--{linkurl/(([^/s]*?)/)}-->)", options);            //聲明賦值標簽(兼容)            r[17] = new Regex(@"<!--{set ((/(?([/w/.<>]+)(?:/)| ))?)(?:/s*)/{?([^/s/{/}]+)/}?(?:/s*)=(?:/s*)(.*?)(?:/s*)}-->", options);            //數據變量標簽            r[18] = new Regex(@"(/{([^/[/]/{/}/s]+)/[([^/[/]/{/}/s]+)/]/})", options);            //普通變量標簽            r[19] = new Regex(@"({([^/[/]//{/}=:'/s]+)})", options);            //時間格式轉換標簽            r[20] = new Regex(@"(<!--{datetostr/(([^/s]+?),(.*?)/)}-->)", options);            //整型轉換標簽            r[21] = new Regex(@"(/{strtoint/(([^/s]+?)/)/})", options);            //直接輸出標簽            r[22] = new Regex(@"<!--{(?:write |=)(?:/s*)(.*?)(?:/s*)}-->", options);

看著一堆啊!主要不怎么會正則就感覺很難。

現在我們在下面方法中怎么使用 主要講一下替換判斷語句if標簽

           string strTemplate=""http://這里放你想替換的模板內容            foreach (Match m in r[7].Matches(strTemplate))            {                IsCodeLine = true;                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),                    "/r/n/tif (" + m.Groups[1].ToString().Replace("http:///"", "/"") + ")/r/n/t{");            }            foreach (Match m in r[8].Matches(strTemplate))            {                IsCodeLine = true;                if (m.Groups[1].ToString() == string.Empty)                {                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),                    "/r/n/t}/r/n/telse/r/n/t{");                }                else                {                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),                        "/r/n/t}/r/n/telse if (" + m.Groups[3].ToString().Replace("http:///"", "/"") + ")/r/n/t{");                }            }            foreach (Match m in r[9].Matches(strTemplate))            {                IsCodeLine = true;                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),                    "/r/n/tif (" + m.Groups[1].ToString().Replace("http:///"", "/"") + ")/r/n/t{");            }            foreach (Match m in r[10].Matches(strTemplate))            {                IsCodeLine = true;                if (m.Groups[1].ToString() == string.Empty)                {                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),                    "/r/n/t}/r/n/telse/r/n/t{");                }                else                {                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),                        "/r/n/t}/r/n/telse if (" + m.Groups[3].ToString().Replace("http:///"", "/"") + ")/r/n/t{");                }            }            

自己寫一個模板引擎就是麻煩,或許直接動態頁面和偽靜態更簡單些。以前都是用的velocity模板引擎,它用起來也很不錯。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海丰县| 抚宁县| 海阳市| 新闻| 江孜县| 郴州市| 油尖旺区| 辽宁省| 大方县| 电白县| 蓝山县| 平南县| 汪清县| 尼勒克县| 长宁县| 洞口县| 和林格尔县| 柳江县| 阿尔山市| 枝江市| 扎鲁特旗| 呼图壁县| 清水河县| 遂川县| 西畴县| 镇康县| 河西区| 宜州市| 镇赉县| 包头市| 汉沽区| 浏阳市| 苍南县| 逊克县| 贵南县| 墨竹工卡县| 康马县| 平南县| 上杭县| 乳山市| 汝阳县|