发个小工具..正则表达示测试器
最近经常用到正则,不过不同语言之间的正则一些特性差别比较大,自已写个小工具,分享下:
下载地址: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;
}
}
}
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;
}
}
}
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;
}
}
}
from:http://www.cnblogs.com/windinwing/archive/2007/11/10/955047.html