我們申明一個數組
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);
看著一堆?。≈饕辉趺磿齽t就感覺很難。
現在我們在下面方法中怎么使用 主要講一下替換判斷語句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模板引擎,它用起來也很不錯。
新聞熱點
疑難解答