asp.net正则模板引擎代码

时间:2023-03-10 04:39:29
asp.net正则模板引擎代码

我们申明一个数组

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

接下来关键的正则表达式:

            RegexOptions options = RegexOptions.None;
//嵌套模板标签(兼容)
r[] = new Regex(@"<!--{template ((skin=\\""([^\[\]\{\}\s]+)\\""(?:\s+))?)src=(?:\/|\\"")([^\[\]\{\}\s]+)(?:\/|\\"")(?:\s*)}-->", options);
//模板路径标签(新增)
r[] = new Regex(@"<!--{templateskin((=(?:\\"")([^\[\]\{\}\s]+)(?:\\""))?)(?:\s*)}-->", options);
//命名空间标签
r[] = new Regex(@"<!--{namespace (?:""?)([\s\S]+?)(?:""?)}-->", options);
//C#代码标签
r[] = new Regex(@"<!--{csharp}-->([\s\S]+?)<!--{/csharp}-->", options);
//loop循环(抛弃)
r[] = new Regex(@"<!--{loop ((\(([^\[\]\{\}\s]+)\) )?)([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+)}-->", options);
//foreach循环(新增)
r[] = new Regex(@"<!--{foreach(?:\s*)\(([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+) in ([^\[\]\{\}\s]+)\)(?:\s*)}-->", options);
//for循环(新增)
r[] = new Regex(@"<!--{for\(([^\(\)\[\]\{\}]+)\)(?:\s*)}-->", options);
//if语句标签(抛弃)
r[] = new Regex(@"<!--{if (?:\s*)(([^\s]+)((?:\s*)(\|\||\&\&)(?:\s*)([^\s]+))?)(?:\s*)}-->", options);
r[] = new Regex(@"<!--{else(( (?:\s*)if (?:\s*)(([^\s]+)((?:\s*)(\|\||\&\&)(?:\s*)([^\s]+))*))?)(?:\s*)}-->", options);
//if语句标签(新增)
r[] = new Regex(@"<!--{if\((([^\s]+)((?:\s*)(\|\||\&\&)(?:\s*)([^\s]+))?)\)(?:\s*)}-->", options);
r[] = new Regex(@"<!--{else(( (?:\s*)if\((([^\s]+)((?:\s*)(\|\||\&\&)(?:\s*)([^\s]+))?))?\))(?:\s*)}-->", options);
//循环与判断结束标签(兼容)
r[] = new Regex(@"<!--{\/(?:loop|foreach|for|if)(?:\s*)}-->", options);
//continue标签
r[] = new Regex(@"<!--{continue(?:\s*)}-->");
//break标签
r[] = new Regex(@"<!--{break(?:\s*)}-->");
//request标签
r[] = new Regex(@"(\{request\[([^\[\]\{\}\s]+)\]\})", options);
//截取字符串标签
r[] = new Regex(@"(<!--{cutstring\(([^\s]+?),(.\d*?)\)}-->)", options);
//url链接标签
r[] = new Regex(@"(<!--{linkurl\(([^\s]*?)\)}-->)", options);
//声明赋值标签(兼容)
r[] = new Regex(@"<!--{set ((\(?([\w\.<>]+)(?:\)| ))?)(?:\s*)\{?([^\s\{\}]+)\}?(?:\s*)=(?:\s*)(.*?)(?:\s*)}-->", options);
//数据变量标签
r[] = new Regex(@"(\{([^\[\]\{\}\s]+)\[([^\[\]\{\}\s]+)\]\})", options);
//普通变量标签
r[] = new Regex(@"({([^\[\]/\{\}=:'\s]+)})", options);
//时间格式转换标签
r[] = new Regex(@"(<!--{datetostr\(([^\s]+?),(.*?)\)}-->)", options);
//整型转换标签
r[] = new Regex(@"(\{strtoint\(([^\s]+?)\)\})", options);
//直接输出标签
r[] = new Regex(@"<!--{(?:write |=)(?:\s*)(.*?)(?:\s*)}-->", options);

看着一堆啊!主要不怎么会正则就感觉很难。

现在我们在下面方法中怎么使用 主要讲一下替换判断语句if标签

           string strTemplate=""//这里放你想替换的模板内容
foreach (Match m in r[].Matches(strTemplate))
{
IsCodeLine = true;
strTemplate = strTemplate.Replace(m.Groups[].ToString(),
"\r\n\tif (" + m.Groups[].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
}
foreach (Match m in r[].Matches(strTemplate))
{
IsCodeLine = true;
if (m.Groups[].ToString() == string.Empty)
{
strTemplate = strTemplate.Replace(m.Groups[].ToString(),
"\r\n\t}\r\n\telse\r\n\t{");
}
else
{
strTemplate = strTemplate.Replace(m.Groups[].ToString(),
"\r\n\t}\r\n\telse if (" + m.Groups[].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
}
}
foreach (Match m in r[].Matches(strTemplate))
{
IsCodeLine = true;
strTemplate = strTemplate.Replace(m.Groups[].ToString(),
"\r\n\tif (" + m.Groups[].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
}
foreach (Match m in r[].Matches(strTemplate))
{
IsCodeLine = true;
if (m.Groups[].ToString() == string.Empty)
{
strTemplate = strTemplate.Replace(m.Groups[].ToString(),
"\r\n\t}\r\n\telse\r\n\t{");
}
else
{
strTemplate = strTemplate.Replace(m.Groups[].ToString(),
"\r\n\t}\r\n\telse if (" + m.Groups[].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
}
}

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