也谈正则表达式

简介:     其实很早就知道了正则表达式,在集成VBScript脚本的时候,就看到了该功能,不过那时觉得很难,觉得也派不上什么用场,所以也没有过多关注。

    其实很早就知道了正则表达式,在集成VBScript脚本的时候,就看到了该功能,不过那时觉得很难,觉得也派不上什么用场,所以也没有过多关注。

最近看了孟岩老师的关于正则表达式讲解,有一种学习正则表达式的冲动,适时我们开发的项目中需要嵌入Python脚本功能,需要一个脚本编辑器,关键字变色等等相关功能用正则表达式实现相对容易的多。

前段时间买了本红皮书《C#字符串和正则表达式参考书》(这真是一本好书,想学习正则表达式的可以参考一下),花了几天的时间把该书看了一遍,正则表达式的用法基本上也弄清楚了,并且对字符串相关的知识也越来越感兴趣了。

对正则表达式的具体规则和使用,实没有什么可说的,网上的文章多的很,都说的比我的好。这是我学习正则表达式做的一个简单正则表达式测试工具,其实大部分代码就是上面书的一个示例(不知道为什么,上网竟没有找到该书的示例源码),又上网查了一些资料,把一些常见的正则表达式也嵌入了进去,方便了正则表达式的应用(以后有时间做一个比较理想的正则表达式工具)。

这是程序的截图:

 

 

 

相关源码:

  // 获取正则表达式的匹配参数
         private  RegexOptions GetSelectedRegexOptions()
        {
            RegexOptions selectedRegexOptions 
=  RegexOptions.None;

            
if  ( this .IgnoreCaseChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.IgnoreCase;
            
if  ( this .ExplicitCaptureChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.ExplicitCapture;
            
if  ( this .ECMAScriptChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.ECMAScript;
            
if  ( this .IgnoreCaseChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.IgnoreCase;
            
if  ( this .MultiLineChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.Multiline;
            
if  ( this .RightToLeftChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.RightToLeft;
            
if  ( this .SingeLineChkBox.Checked)
                selectedRegexOptions 
|=  RegexOptions.Singleline;
            
return  selectedRegexOptions;

        }

        
private   void  TestRegexButton_Click( object  sender, EventArgs e)
        {
            
try
            {
                RegexOptions selectedRegexOptions 
=   this .GetSelectedRegexOptions();
                Regex testRegex 
=   new  Regex( this .RegexTextBox.Text, selectedRegexOptions);
                
if  (testRegex.IsMatch( this .InputTextBox.Text))
                {
                    
this .ResultsTextBox.ForeColor  =  Color.Black;
                    
this .ResultsTextBox.Text  =   " 匹配成功 " ;
                }
                
else
                {
                    
this .ResultsTextBox.ForeColor  =  Color.Red;
                    
this .ResultsTextBox.Text  =   " 匹配失败 " ;
                }
            }
            
catch  (ArgumentException ex)
            {
                
this .ResultsTextBox.ForeColor  =  Color.Red;
                
this .ResultsTextBox.Text  =   " 错误: " + ex.Message;
            }
        }

        
private   void  ReplaceButton_Click( object  sender, EventArgs e)
        {
            
try
            {
                RegexOptions selectedRegexOptions 
=   this .GetSelectedRegexOptions();
                Regex replaceRegex 
=   new  Regex( this .RegexTextBox.Text, selectedRegexOptions);

                
this .ResultsTextBox.ForeColor  =  Color.Black;
                
this .ResultsTextBox.Text  =  replaceRegex.Replace( this .InputTextBox.Text,  this .ReplacementTextBox.Text);
                              
            }
            
catch  (ArgumentException ex)
            {
                
this .ResultsTextBox.ForeColor  =  Color.Red;
                
this .ResultsTextBox.Text  =   " 错误: "   +  ex.Message;
            }
        }

        
private   void  SplitBoutton_Click( object  sender, EventArgs e)
        {
            
try
            {
                RegexOptions selectedRegexOptions 
=   this .GetSelectedRegexOptions();
                Regex splitRegex 
=   new  Regex( this .RegexTextBox.Text, selectedRegexOptions);

                String[] splitResults;
                splitResults 
=  splitRegex.Split( this .InputTextBox.Text);
                StringBuilder resultsString 
=   new  StringBuilder( this .InputTextBox.Text.Length);

                
foreach  (String stringElement  in  splitResults)
                    resultsString.Append(stringElement 
+  Environment.NewLine);

                
this .ResultsTextBox.ForeColor  =  Color.Black;
                
this .ResultsTextBox.Text  =  resultsString.ToString();
            }
            
catch  (ArgumentException ex)
            {
                
this .ResultsTextBox.ForeColor  =  Color.Red;
                
this .ResultsTextBox.Text  =   " 错误: "   +  ex.Message;
            }            

        }

        
private   void  MatchesButton_Click( object  sender, EventArgs e)
        {
            
try
            {
                RegexOptions selectedRegexOptions 
=   this .GetSelectedRegexOptions();
                Regex matchesRegex 
=   new  Regex( this .RegexTextBox.Text, selectedRegexOptions);

                MatchCollection matchesFound;
                matchesFound 
=  matchesRegex.Matches( this .InputTextBox.Text);

                String nextMath 
=   " ------- 下一个匹配项 ---------- " ;
                StringBuilder resultsString 
=   new  StringBuilder( 64 );

                
foreach (Match matchMode  in  matchesFound)
                    resultsString.Append(matchMode.Value 
+ (Environment.NewLine + nextMath));

                
this .ResultsTextBox.ForeColor  =  Color.Black;
                
this .ResultsTextBox.Text  =  resultsString.ToString();
            }
            
catch  (ArgumentException ex)
            {
                
this .ResultsTextBox.ForeColor  =  Color.Red;
                
this .ResultsTextBox.Text  =   " 错误: "   +  ex.Message;
            }            

        }

        
private   void  GroupsButton_Click( object  sender, EventArgs e)
        {
            
try
            {
                RegexOptions selectedRegexOptions 
=   this .GetSelectedRegexOptions();
                Regex matchesRegex 
=   new  Regex( this .RegexTextBox.Text, selectedRegexOptions);

                MatchCollection matchesFound;
                matchesFound 
=  matchesRegex.Matches( this .InputTextBox.Text);

                String nextMath 
=   " ------- 下一个分组 ---------- " ;
                StringBuilder resultsString 
=   new  StringBuilder( 64 );
                GroupCollection matchGroups;


                
foreach  (Match matchMode  in  matchesFound)
                {
                    matchGroups 
=  matchMode.Groups;
                    
foreach  (Group matchGroup  in  matchGroups)
                        resultsString.Append(
" ( "   +  matchGroup.Value  +   " ) " );
                    resultsString.Append(Environment.NewLine 
+  nextMath);
                }

                
this .ResultsTextBox.ForeColor  =  Color.Black;
                
this .ResultsTextBox.Text  =  resultsString.ToString();
            }
            
catch  (ArgumentException ex)
            {
                
this .ResultsTextBox.ForeColor  =  Color.Red;
                
this .ResultsTextBox.Text  =   " 错误: "   +  ex.Message;
            }           
        }

        
// 常见正则表达式
         private   void  FamiliarRegex_LinkClicked( object  sender, LinkLabelLinkClickedEventArgs e)
        {
            MenuRegex.Show(
this , new  Point(FamiliarRegex.Location.X, FamiliarRegex.Location.Y  +  FamiliarRegex.Size.Height));  
            
// MessageBox.Show("sdfsd");
        }

        
private   void  MenuRegexItem_Click( object  sender, EventArgs e)
        {
            
string  strRegex  =   "" ;
            
switch  (((ToolStripMenuItem)sender).Text)
            {
                
case   " 整数 " :
                    strRegex 
=   @" ^((+|-)d)?d*$ " ;
                    
break ;
                
case   " 浮点数 " :
                    strRegex 
=   @" ^(?:+|-)?d+(?:.d+)?$ " ;
                    
break ;
                
case   " 电话号码 " :
                    strRegex 
=   @" d{3}-d{8}|d{4}-d{7} " ;
                    
break ;
                
case   " 邮政编码 " :
                    strRegex 
=   @" [1-9]d{5}(?!d) " ;
                    
break ;
                
case   " Email地址1 " :
                    strRegex 
=   @" ^(([^<>()[]/.,;:@ " + ' " ' + @"

 

下载地址:http://download.csdn.net/source/162304

 

相关文章
|
11天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
10天前
|
存储 人工智能 搜索推荐
终身学习型智能体
当前人工智能前沿研究的一个重要方向:构建能够自主学习、调用工具、积累经验的小型智能体(Agent)。 我们可以称这种系统为“终身学习型智能体”或“自适应认知代理”。它的设计理念就是: 不靠庞大的内置知识取胜,而是依靠高效的推理能力 + 动态获取知识的能力 + 经验积累机制。
356 131
|
10天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
443 131
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
4天前
|
存储 安全 前端开发
如何将加密和解密函数应用到实际项目中?
如何将加密和解密函数应用到实际项目中?
206 138
|
10天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
405 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
4天前
|
存储 JSON 安全
加密和解密函数的具体实现代码
加密和解密函数的具体实现代码
204 136
|
22天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1363 8
|
9天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。