正则表达式构造与测试小工具(下)

简介:



不多说废话了,直接贴一些重要的代码:
(1)查找匹配项:

        private void RunMatch()
        {
            Regex r;
            Match m;

            statusBar.Panels[0].Text="";
            statusBar.Panels[1].Text="";
            statusBar.Panels[2].Text="";

            InputBox.Select(0,0);   // Unselect all the text
            Dirty=false;
            Skip=true;

            this.Cursor=Cursors.WaitCursor;
            if((r=MakeRegex())==null)
            {
                this.Cursor=Cursors.Default;
                return;
            }

            Tree.Nodes.Clear();
            ResultsBox.Text="";
            ShowBuilder(false);
            ShowTree(true);
            this.Cursor=Cursors.Default;

            // Store the results in the text box
            for (m = r.Match(InputBox.Text); m.Success; m = m.NextMatch()) 
            {
                if(m.Value.Length>0)
                {
                    Tree.Nodes.Add("["+m.Value + "]");
                    int ThisNode=Tree.Nodes.Count-1;
                    Tree.Nodes[ThisNode].Tag=m;
                    if(m.Groups.Count>1)
                    {
                        for (int i=1;i<m.Groups.Count;i++)
                        {
                            Tree.Nodes[ThisNode].Nodes.Add(r.GroupNameFromNumber(i)+": ["+m.Groups[i].Value+"]");
                            Tree.Nodes[ThisNode].Nodes[i-1].Tag=m.Groups[i];
              //This bit of code puts in another level of nodes showing the captures for each group
                            int Number=m.Groups[i].Captures.Count;
                            if(Number>1)
                                for(int j=0;j<Number;j++)
                                {
                                    Tree.Nodes[ThisNode].Nodes[i-1].Nodes.Add(m.Groups[i].Captures[j].Value);
                                    Tree.Nodes[ThisNode].Nodes[i-1].Nodes[j].Tag=m.Groups[i].Captures[j];
                                }
                        }
                    }
                }
            }
            statusBar.Panels[0].Text=Tree.Nodes.Count.ToString()+" Matches";
            Skip=false;
        }

(2)替换匹配项
        private void RunReplace()
        {
            Regex r;
            this.Cursor=Cursors.WaitCursor;
            statusBar.Panels[0].Text="";
            statusBar.Panels[1].Text="";
            statusBar.Panels[2].Text="";

            InputBox.Select(0,0);   // Unselect all the text
            Dirty=false;
            Skip=true;

            if((r=MakeRegex())==null)
            {
                this.Cursor=Cursors.Default;
                return;
            }

            Tree.Nodes.Clear();
            ShowBuilder(false);
            ShowTree(false);
            this.Cursor=Cursors.Default;
            ResultsBox.Text=r.Replace(InputBox.Text,Replace.Text);
            Skip=false;
            statusBar.Panels[0].Text="";
        }
(3)保存项目信息的两个类
    [Serializable]
    public class Settings
    {
        public string FileName="TheRegexAssembly.dll";
        public string ClassName="TheRegexClass";
        public string Namespace="TheRegex";
        public bool IsPublic=true;
        public string InputData= "";
        public string RegularExpression = "";
        public string ReplacementString = "";
        public bool IgnoreCase = true;
        public bool Multiline = true;
        public bool Singleline = false;
        public bool ExplicitCapture = false;
        public bool ECMAScript = false;
        public bool RightToLeft = false;
        public bool IgnorePatternWS = true;
        public bool Compiled = true;
        public int LeftPanelWidth = 470;
        public int TreeHeight = 150;
        public Size Size = new Size(800, 400);
    }

    public class RegistrySettings
    {
        public string[] MRUList = new string[0];
        public Point Location = new Point(100,100);
        public string ProjectFile ="";
        public string OpenPathName="";
        public string SavePathName="";
    }
(4)保存项目
    private bool SaveFileData()
        {
            settings.InputData=InputBox.Text;
            settings.IsPublic=MakeForm.IsPublic;
            settings.Namespace=MakeForm.Namespace;
            settings.ClassName=MakeForm.ClassName;
            settings.InputData=InputBox.Text;
            settings.ReplacementString=Replace.Text;
            settings.RegularExpression=RegexBox.Text;
            settings.IgnoreCase=IgnoreCase.Checked;
            settings.Multiline=Multiline.Checked;
            settings.Singleline=Singleline.Checked;
            settings.ExplicitCapture=Explicit.Checked;
            settings.ECMAScript=ECMA.Checked;
            settings.RightToLeft=RightToLeftBox.Checked;
            settings.IgnorePatternWS=IgnorePattern.Checked;
            settings.Compiled=Compiled.Checked;
            settings.LeftPanelWidth=LeftPanel.Width;
            if(IsVisible)settings.TreeHeight=Tree.Height;
            else settings.TreeHeight=SaveTreeHeight;
            settings.Size=this.Size;
            if(Savior.SaveToFile(settings, ProjectFileName))return true;
            else return false;
        }

(5)从设置文件中读取信息
        private bool ReadFileData()
        {
            settings = (Settings)Savior.ReadFromFile(settings,ProjectFileName);
            if(settings==null)return false;
            InputBox.Text=settings.InputData;
            Replace.Text=settings.ReplacementString;
            RegexBox.Text=settings.RegularExpression;
            MakeForm.FileName=settings.FileName;
            MakeForm.IsPublic=settings.IsPublic;
            MakeForm.Namespace=settings.Namespace;
            MakeForm.ClassName=settings.ClassName;
            IgnoreCase.Checked=settings.IgnoreCase;
            Multiline.Checked=settings.Multiline;
            Singleline.Checked=settings.Singleline;
            Explicit.Checked=settings.ExplicitCapture;
            ECMA.Checked=settings.ECMAScript;
            RightToLeftBox.Checked=settings.RightToLeft;
            IgnorePattern.Checked=settings.IgnorePatternWS;
            Compiled.Checked=settings.Compiled;
            LeftPanel.Width=settings.LeftPanelWidth;
            Tree.Height=settings.TreeHeight;
            SaveTreeHeight=Tree.Height;
            this.Size=settings.Size;
            return true;
        }
(6)树结点选择
private void Tree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            if(Skip)return;
            if(Tree.SelectedNode.Parent==null) // Must be the top level node
            {
                Match m=(Match)Tree.SelectedNode.Tag;
                ResultsBox.Text=m.Value;
                statusBar.Panels[1].Text="Position: "+m.Index;
                statusBar.Panels[2].Text="Length: "+m.Length;
                if(m!=null && !Dirty)
                {
                    InputBox.Select(m.Index,m.Length);
                    InputBox.ScrollToCaret();
                }
            }
            else if(Tree.SelectedNode.Parent.Parent==null) // Must be a group
            {
                Group g=(Group)Tree.SelectedNode.Tag;
                ResultsBox.Text=g.Value;
                statusBar.Panels[1].Text="Position: "+g.Index;
                statusBar.Panels[2].Text="Length: "+g.Length;
                if(g!=null && !Dirty)
                {
                    InputBox.Select(g.Index,g.Length);
                    InputBox.ScrollToCaret();
                }
            }
            else // Must be a capture
            {
                Capture c=(Capture)Tree.SelectedNode.Tag;
                ResultsBox.Text=c.Value;
                statusBar.Panels[1].Text="Position: "+c.Index;
                statusBar.Panels[2].Text="Length: "+c.Length;
                if(c!=null && !Dirty)
                {
                    InputBox.Select(c.Index,c.Length);
                    InputBox.ScrollToCaret();
                }
            }
        }

自动构建正则表达式我也见识过一个这样的工具,不过我对其实现机制还是不大理解,慢慢再说吧,就写这里了,太累了,。。。



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/08/01/465331.html,如需转载请自行联系原作者
目录
相关文章
|
10天前
|
机器学习/深度学习 人工智能 测试技术
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
EdgeMark是一个面向嵌入式AI的自动化部署与基准测试系统,支持TensorFlow Lite Micro、Edge Impulse等主流工具,通过模块化架构实现模型生成、优化、转换与部署全流程自动化,并提供跨平台性能对比,助力开发者在资源受限设备上高效选择与部署AI模型。
125 9
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
|
17天前
|
Java 测试技术 API
自动化测试工具集成及实践
自动化测试用例的覆盖度及关键点最佳实践、自动化测试工具、集成方法、自动化脚本编写等(兼容多语言(Java、Python、Go、C++、C#等)、多框架(Spring、React、Vue等))
60 6
|
2月前
|
前端开发 Java jenkins
Jmeter压力测试工具全面教程和使用技巧。
JMeter是一个能够模拟高并发请求以检查应用程序各方面性能的工具,包括但不限于前端页面、后端服务及数据库系统。熟练使用JMeter不仅能够帮助发现性能瓶颈,还能在软件开发早期就预测系统在面对真实用户压力时的表现,确保软件质量和用户体验。在上述介绍的基础上,建议读者结合官方文档和社区最佳实践,持续深入学习和应用。
509 10
|
2月前
|
监控 Java 数据挖掘
利用Jmeter工具进行HTTP接口的性能测试操作
基础上述步骤反复迭代调整直至满足预期目标达成满意水平结束本轮压力评估周期进入常态监控阶段持续关注系统运转状态及时发现处理新出现问题保障服务稳定高效运作
294 0
|
3月前
|
敏捷开发 运维 数据可视化
DevOps看板工具中的协作功能:如何打破开发、测试与运维之间的沟通壁垒
在DevOps实践中,看板工具通过可视化任务管理和自动化流程,提升开发与运维团队的协作效率。它支持敏捷开发、持续交付,助力团队高效应对需求变化,实现跨职能协作与流程优化。
|
4月前
|
数据可视化 测试技术 Go
Go 语言测试与调试:`go test` 工具用法
`go test` 是 Go 语言内置的测试工具,支持单元测试、基准测试、示例测试等功能。本文详解其常用参数、调试技巧及性能测试命令,并提供实际项目中的应用示例与最佳实践。
|
3月前
|
人工智能 数据可视化 测试技术
UAT测试排程工具深度解析:让验收测试不再失控,项目稳稳上线
在系统交付节奏加快的背景下,“测试节奏混乱”已成为项目延期的主因之一。UAT测试排程工具应运而生,帮助团队结构化拆解任务、清晰分配责任、实时掌控进度,打通需求、测试、开发三方协作闭环,提升测试效率与质量。本文还盘点了2025年热门UAT工具,助力团队选型落地,告别靠表格和群聊推进测试的低效方式,实现有节奏、有章法的测试管理。
|
数据库 Python
Python网络数据抓取(8):正则表达式
Python网络数据抓取(8):正则表达式
|
自然语言处理 JavaScript 前端开发
Python高级语法与正则表达式(二)
正则表达式描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。
|
安全 算法 Python
Python高级语法与正则表达式(一)
Python提供了 with 语句的写法,既简单又安全。 文件操作的时候使用with语句可以自动调用关闭文件操作,即使出现异常也会自动关闭文件操作。

热门文章

最新文章