也谈正则表达式

news/2024/7/1 21:00:50

    其实很早就知道了正则表达式,在集成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

 





http://www.niftyadmin.cn/n/3655764.html

相关文章

内部属性[[class]]是什么?

所有的 typeof 返回值为 “object” 的对象&#xff08;如数组&#xff09;都包含一个内部属性 [[class]] (我们可以把它看作一个内部的分类)&#xff0c;而非传统的面向对象意义上的类&#xff09;。 这个属性无法直接被访问&#xff0c;一般通过 object。prototype。toStrin…

WinCE5.0平台下的Moxa DA66x设备应用开发心得

最近金日隧道广告系统的通信系统要升级&#xff0c;用Moxa的DA66x设备取代原先的Moxa5630通信模块&#xff0c;由于DA66x内嵌了WinCE5.0系统&#xff0c;系统的功能可以有很大的发挥余地。DA66x这款产品功能很强&#xff0c;可以说WinCE5.0在工业通信领域被用到了极致&#xff…

介绍 js 有哪些内置对象?

涉及的知识点 js 中的内置对象主要指的是在程序执行前存在全局作用域里的由 js 定义的一些全局值属性、函数和用来实例化其他对象的构造函 数对象。一般我们经常用到的如全局变量值 NaN、undefined&#xff0c;全局函数如 parseInt()、parseFloat() 用来实例化对象的构 造函数…

Windows Vista不兼容VS2005(需打补丁)

在Windows Vista平台上直接安装VS2005&#xff0c;没有想到安装程序直接就提示Vista与VS2005存在已知的兼容问题&#xff0c;看来需要打SP1补丁(该补丁虽然下载了&#xff0c;但是在XP安装巨慢&#xff0c;不知道在vista上品行如何&#xff09;&#xff0c;此外SQL Server 2005…

解构赋值笔记

数组的解构赋值 let arr [1,2,3,4,5] let [item1,item2] arr console.log(item1,item2) // 1,2 数据2之后的345被垃圾回收机制回收 let [item1, item2,...list] arr console.log(list) // 3,4,5 利用... 把其他的数据赋值到list上 ""..."" 用于取出…

Office 2007 中文版快速一览

Office2007中文版同样是从MSDN下载的&#xff0c;使用起来和office2003以前版本有很大不同&#xff0c;绝对有震撼效果&#xff08;相比Windows Vista 之于 Windows XP还要强&#xff09;&#xff0c;UI设计的效果和理念&#xff0c;绝对在业界又掀起一股仿制狂潮&#xff01;这…

Callback 异步操作

1. 什么是回调地狱? 在业务逻辑中为了实现业务功能,经常需要函数层层嵌套的回调函数,如果嵌套过多,极大影响代码可读性和逻辑,这样的场景叫做回调地狱 var sayhello function (name, callback) {setTimeout(function () {console.log(name);callback();}, 1000); } sayhell…

Windows Vista 中文版的兼容性

想把工作平台逐渐有Windows XP 转移 到windows Vista 上来&#xff0c;所以在该平台上开始安装一些Windows XP平台上的程序&#xff0c;目前的情况如下&#xff1a;1、紫光拼音3.0 &#xff0d; 安装成功 2、瑞星杀毒2007 &#xff0d; 安装成功 3、瑞星防火墙2007 …