正则表达示测试器

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

最近经常用到正则,不过不同语言之间的正则一些特性差别比较大,自已写个小工具,分享下:
下载地址: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,当然复杂的就不适用了,需要用环视判断字符边界,另外还要区分字符串中的转义符..
from:http://www.cnblogs.com/windinwing/archive/2007/11/10/955047.html
原文地址:https://www.cnblogs.com/smallfa/p/956360.html