asp.net正則模板引擎代碼

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   資料   io   for   

我們申明一個數組

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

接下來關鍵的Regex:

            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=""//這裡放你想替換的模板內容            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模板引擎,它用起來也很不錯。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.