叶帆工作室

嵌入式开发爱好者(十年开发经验,精通C/C++/VC/VB/C#...)

刘洪峰ID:yefanqiu
522234次访问,排名81好友0人,关注者170
微软MVP / CSDN 2008十大MVB/MSDN中文技术论坛版主
yefanqiu的文章
原创 216 篇
翻译 0 篇
转载 3 篇
评论 1089 篇
叶帆的公告
本博客原创文章,作者保留一切权利,需经作者同意后方可转载,转载时 请注明[叶帆工作室]及文章链接。yefan@vip.sina.com
【简介】叶帆[微软MVP]
【文章】叶帆文章列表
【软件】叶帆共享软件列表
最近评论
mulingfeng:-------- SOS
我要想用 asp.net做一个类似这样的url重定向 如何做
eg:http://xxx.xx.com/feng
给这种访问方式做一个重定向 如何做 ?
mulingfeng:请问 我要想用 asp.net做一个类似这样的url重定向 如何做
eg:http://xxx.xx.com/feng
给这种访问方式做一个重定向 如何做 ?
jacle169:峰哥, micro Framework3.0 beta出了,我已经装到vs08里了,但是你的sdk不能注册到vs08里,你帮帮忙改个vs08 里能用的sdk吧,谢谢.
赵广涛:高手帮帮忙,VB画有两个Y轴的曲线图怎么画?
sfweb2008:老兄:
你真厉害!
你有VB Api函数功能及用法详解吗?我有好多这方面的问题不是很明白!还请指教,谢谢!

我的邮箱xsdjxx@tom.com
文章分类
收藏
    相册
    叶帆照片
    【叶帆软件】
    [01]VB源码之友(V2.1.548)
    [02]API浏览器.net(V5.0)
    [03]叶帆成语词典(V2.0.8)
    [04]叶帆密码库(V1.2.8)
    【叶帆资源】
    DAO 2.0引擎
    叶帆快速通道
    Windows Embedded 专题
    中文MSDN
    叶帆圈子--工业自动化
    叶帆工作室(博客园)
    叶帆工控--工业自动化
    叶帆群组--工业应用开发
    微软中文技术论坛
    瑞康社区论坛
    叶帆友情链接
    张欣
    枕善居
    莫依
    葛涵涛
    郑建
    陈辉
    马宁
    马骐
    魏涛序
    黎波
    存档
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 也谈正则表达式收藏

    新一篇: 初涉龙芯I/龙芯II | 旧一篇: .net精简框架集下的ini文件读取(C#)

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

     

    发表于 @ 2007年03月20日 21:38:00|评论(loading...)|编辑

    新一篇: 初涉龙芯I/龙芯II | 旧一篇: .net精简框架集下的ini文件读取(C#)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 叶帆