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 (?:""?)([sS]+?)(?:""?)}-->", options);
            //C#代码标签
            r[3] = new Regex(@"<!--{csharp}-->([sS]+?)<!--{/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(),
                    "
	if (" + m.Groups[1].ToString().Replace("\"", """) + ")
	{");
            }
            foreach (Match m in r[8].Matches(strTemplate))
            {
                IsCodeLine = true;
                if (m.Groups[1].ToString() == string.Empty)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                    "
	}
	else
	{");
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                        "
	}
	else if (" + m.Groups[3].ToString().Replace("\"", """) + ")
	{");
                }
            }
            foreach (Match m in r[9].Matches(strTemplate))
            {
                IsCodeLine = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                    "
	if (" + m.Groups[1].ToString().Replace("\"", """) + ")
	{");
            }
            foreach (Match m in r[10].Matches(strTemplate))
            {
                IsCodeLine = true;
                if (m.Groups[1].ToString() == string.Empty)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                    "
	}
	else
	{");
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                        "
	}
	else if (" + m.Groups[3].ToString().Replace("\"", """) + ")
	{");
                }
            }
            

自己写一个模板引擎就是麻烦,或许直接动态页面和伪静态更简单些。以前都是用的velocity模板引擎,它用起来也很不错。

原文地址:https://www.cnblogs.com/angelasp/p/3865591.html