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);
复制代码

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

现在我们在下面方法中怎么使用 主要讲一下替换判断语句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模板引擎,它用起来也很不错。

posted @   Angelasp  阅读(1256)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示