国产探花免费观看_亚洲丰满少妇自慰呻吟_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模板引擎,它用起來也很不錯。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜良县| 江华| 鄂尔多斯市| 灵寿县| 汕头市| 玛多县| 分宜县| 濉溪县| 绥江县| 龙泉市| 敖汉旗| 正阳县| 海丰县| 双流县| 望江县| 高雄县| 富宁县| 高平市| 湾仔区| 周宁县| 察雅县| 南皮县| 商水县| 治多县| 金昌市| 兴和县| 镇赉县| 锡林浩特市| 柳江县| 明光市| 拜泉县| 敖汉旗| 临沂市| 永仁县| 噶尔县| 天祝| 玛纳斯县| 贺兰县| 洛浦县| 贺兰县| 揭东县|