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);

看着一堆啊!主要不怎麼會正則就感受很難。oop

如今咱們在下面方法中怎麼使用 主要講一下替換判斷語句if標籤url

           string strTemplate=""//這裏放你想替換的模板內容
            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("\\\"", "\"") + ")\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("\\\"", "\"") + ")\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("\\\"", "\"") + ")\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("\\\"", "\"") + ")\r\n\t{");
                }
            }
            

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

相關文章
相關標籤/搜索