发个小工具..正则表达示测试器

最近经常用到正则,不过不同语言之间的正则一些特性差别比较大,自已写个小工具,分享下:
下载地址:http://xiaoxiao.bfor.cn/download/regex.rar
按惯例,上几张图:

 

最后那个,生成代码,使用RichTextBox对代码着色分二步:
关键字着色:

  
   // 定义关键字
             string [] keys  =   new   string []{ " using " , " return " , " if " , " else " , " public " };
            
foreach  ( string  key  in  keys)
            {
                Regex r 
=   new  Regex(key);
                MatchCollection mc 
=  r.Matches(rtxtCode.Text);
                
foreach  (Match m  in  mc)
                {
                    
if  (m.Success) // 当匹配成功,对关键字着色
                    {   
                        rtxtCode.Select(m.Index, m.Length);
                        rtxtCode.SelectionColor 
=  Color.Blue;
                    }
                }
            }

            

字符串着色这个要单独来:

Regex r1  =   new  Regex( " \ " ( ?< string > [ ^ \ " ]*)\ "" );
            MatchCollection mc1  =  r1.Matches(rtxtCode.Text);
            
foreach  (Match m  in  mc1)
            {
                
if  (m.Success)
                {
                    Group g 
=  m.Groups[ " string " ];
                    
if  (g.Success)
                    {
                        rtxtCode.Select(g.Index
- 1 , g.Length + 2 );
                        rtxtCode.SelectionColor 
=  Color.Red;
                    }
                }
            }

方法是查找字符串索引,右边去掉一个字符,右边加2,当然复杂的就不适用了,需要用环视判断字符边界,另外还要区分字符串中的转义符..

转载于:https://www.cnblogs.com/windinwing/archive/2007/11/10/955047.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值