• 正则表达示在ASP.NET中用来生成静态网页的使用


     static void Main() {
                // Create proxy and request a service
                //Proxy proxy = new Proxy();
                //proxy.Request();
                // Wait for user
                GenerationStaticPage p = new GenerationStaticPage();
                string pagecontent = p.GenerationPage();
                Console.Write(pagecontent);
                Console.Read();
               
            }
            public class GenerationStaticPage
            {
                #region 属性
                public string MemberName
                {
                    get
                    {
                        return "Vodgo";
                    }
                    set
                    {
                        //
                    }

                }
                public string MeetingName
                {
                    get
                    {
                        return "Productor page";
                    }
                    set
                    {
                        //
                    }

                }

                public string RoomName
                {
                    get
                    {
                        return "24-1";
                    }
                    set
                    {
                        //
                    }

                }

                public string BeginTime
                {
                    get
                    {
                        return "2007-12-1 12:01:29";
                    }
                    set
                    {
                        //
                    }

                }
                #endregion

                public string GenerationPage()
                {

                    //读取模板的内容(方法自己写吧)
                    string content = "你好,#?MemberName?#请出席会议:#?MeetingName?#。地点:#?RoomName?#。时间:#?BeginTime?#。";
                    StringBuilder builder = new StringBuilder(content);
                    string pattern = @"#\?(?'property'\S+?)\?#";
                    return Regex.Replace(content, pattern, new MatchEvaluator(RegexMatchEvaluation), RegexOptions.ExplicitCapture);
                }

                private string RegexMatchEvaluation(Match match)
                {
                    //get the property name (named group of the regex)
                    string propertyName = match.Groups["property"].Value;

                    //try to get a property handle from the business object
                    PropertyInfo pi = this.GetType().GetProperty(propertyName);

                    //do not replace anything if no such property exists
                    if (pi == null)
                    {
                        return match.Value;
                    }

                    //return the property value
                    object propertyValue = pi.GetValue(this, null);
                    if (propertyValue != null)
                    {
                        return propertyValue.ToString();
                    }
                    else
                    {
                        return string.Empty;
                    }
                }

            }     
    比较模板内容:
    你好,#?MemberName?#请出席会议:#?MeetingName?#。地点:#?RoomName?#。时间:#?BeginTime?#。
    运行结果:
    你好,Vodgo请出席会议:Productor page。地点:24-1。时间:2007-12-1 12:01:29。


  • 相关阅读:
    自动化测试基础篇--Selenium文件上传send_keys
    自动化测试基础篇--Selenium中数据参数化之TXT
    自动化测试基础篇--Selenium中JS处理浏览器弹窗
    自动化测试基础篇--Selenium中JS处理滚动条
    自动化测试基础篇--Selenium单选框(Radio)复选框(CheckBox)
    自动化测试基础篇--Selenium弹出框alert
    自动化测试基础篇--Selenium select下拉框
    自动化测试基础篇--Selenium多窗口、句柄问题
    flask初探
    windows系统在python3.5环境下安装mysql组件
  • 原文地址:https://www.cnblogs.com/sgciviolence/p/1154351.html
Copyright © 2020-2023  润新知