正则表达式构造与测试小工具(下)

本文介绍了一个正则表达式工具的关键代码实现,包括查找匹配项、替换匹配项的功能,并展示了如何通过树形结构展示匹配结果,同时提供了保存和读取项目设置的方法。
200673101.JPG


不多说废话了,直接贴一些重要的代码:
(1)查找匹配项:

None.gifprivatevoidRunMatch()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifRegexr;
InBlock.gifMatchm;
InBlock.gif
InBlock.gifstatusBar.Panels[
0].Text="";
InBlock.gifstatusBar.Panels[
1].Text="";
InBlock.gifstatusBar.Panels[
2].Text="";
InBlock.gif
InBlock.gifInputBox.Select(
0,0);//Unselectallthetext
InBlock.gif
Dirty=false;
InBlock.gifSkip
=true;
InBlock.gif
InBlock.gif
this.Cursor=Cursors.WaitCursor;
InBlock.gif
if((r=MakeRegex())==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.Cursor=Cursors.Default;
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifTree.Nodes.Clear();
InBlock.gifResultsBox.Text
="";
InBlock.gifShowBuilder(
false);
InBlock.gifShowTree(
true);
InBlock.gif
this.Cursor=Cursors.Default;
InBlock.gif
InBlock.gif
//Storetheresultsinthetextbox
InBlock.gif
for(m=r.Match(InputBox.Text);m.Success;m=m.NextMatch())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(m.Value.Length>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifTree.Nodes.Add(
"["+m.Value+"]");
InBlock.gif
intThisNode=Tree.Nodes.Count-1;
InBlock.gifTree.Nodes[ThisNode].Tag
=m;
InBlock.gif
if(m.Groups.Count>1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(inti=1;i<m.Groups.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifTree.Nodes[ThisNode].Nodes.Add(r.GroupNameFromNumber(i)
+":["+m.Groups[i].Value+"]");
InBlock.gifTree.Nodes[ThisNode].Nodes[i
-1].Tag=m.Groups[i];
InBlock.gif
//Thisbitofcodeputsinanotherlevelofnodesshowingthecapturesforeachgroup
InBlock.gif
intNumber=m.Groups[i].Captures.Count;
InBlock.gif
if(Number>1)
InBlock.gif
for(intj=0;j<Number;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifTree.Nodes[ThisNode].Nodes[i
-1].Nodes.Add(m.Groups[i].Captures[j].Value);
InBlock.gifTree.Nodes[ThisNode].Nodes[i
-1].Nodes[j].Tag=m.Groups[i].Captures[j];
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifstatusBar.Panels[
0].Text=Tree.Nodes.Count.ToString()+"Matches";
InBlock.gifSkip
=false;
ExpandedBlockEnd.gif}

(2)替换匹配项
None.gifprivatevoidRunReplace()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifRegexr;
InBlock.gif
this.Cursor=Cursors.WaitCursor;
InBlock.gifstatusBar.Panels[
0].Text="";
InBlock.gifstatusBar.Panels[
1].Text="";
InBlock.gifstatusBar.Panels[
2].Text="";
InBlock.gif
InBlock.gifInputBox.Select(
0,0);//Unselectallthetext
InBlock.gif
Dirty=false;
InBlock.gifSkip
=true;
InBlock.gif
InBlock.gif
if((r=MakeRegex())==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.Cursor=Cursors.Default;
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifTree.Nodes.Clear();
InBlock.gifShowBuilder(
false);
InBlock.gifShowTree(
false);
InBlock.gif
this.Cursor=Cursors.Default;
InBlock.gifResultsBox.Text
=r.Replace(InputBox.Text,Replace.Text);
InBlock.gifSkip
=false;
InBlock.gifstatusBar.Panels[
0].Text="";
ExpandedBlockEnd.gif}
(3)保存项目信息的两个类
None.gif[Serializable]
None.gif
publicclassSettings
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
publicstringFileName="TheRegexAssembly.dll";
InBlock.gif
publicstringClassName="TheRegexClass";
InBlock.gif
publicstringNamespace="TheRegex";
InBlock.gif
publicboolIsPublic=true;
InBlock.gif
publicstringInputData="";
InBlock.gif
publicstringRegularExpression="";
InBlock.gif
publicstringReplacementString="";
InBlock.gif
publicboolIgnoreCase=true;
InBlock.gif
publicboolMultiline=true;
InBlock.gif
publicboolSingleline=false;
InBlock.gif
publicboolExplicitCapture=false;
InBlock.gif
publicboolECMAScript=false;
InBlock.gif
publicboolRightToLeft=false;
InBlock.gif
publicboolIgnorePatternWS=true;
InBlock.gif
publicboolCompiled=true;
InBlock.gif
publicintLeftPanelWidth=470;
InBlock.gif
publicintTreeHeight=150;
InBlock.gif
publicSizeSize=newSize(800,400);
ExpandedBlockEnd.gif}

None.gif
None.gif
publicclassRegistrySettings
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
publicstring[]MRUList=newstring[0];
InBlock.gif
publicPointLocation=newPoint(100,100);
InBlock.gif
publicstringProjectFile="";
InBlock.gif
publicstringOpenPathName="";
InBlock.gif
publicstringSavePathName="";
ExpandedBlockEnd.gif}
(4)保存项目
None.gifprivateboolSaveFileData()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifsettings.InputData
=InputBox.Text;
InBlock.gifsettings.IsPublic
=MakeForm.IsPublic;
InBlock.gifsettings.Namespace
=MakeForm.Namespace;
InBlock.gifsettings.ClassName
=MakeForm.ClassName;
InBlock.gifsettings.InputData
=InputBox.Text;
InBlock.gifsettings.ReplacementString
=Replace.Text;
InBlock.gifsettings.RegularExpression
=RegexBox.Text;
InBlock.gifsettings.IgnoreCase
=IgnoreCase.Checked;
InBlock.gifsettings.Multiline
=Multiline.Checked;
InBlock.gifsettings.Singleline
=Singleline.Checked;
InBlock.gifsettings.ExplicitCapture
=Explicit.Checked;
InBlock.gifsettings.ECMAScript
=ECMA.Checked;
InBlock.gifsettings.RightToLeft
=RightToLeftBox.Checked;
InBlock.gifsettings.IgnorePatternWS
=IgnorePattern.Checked;
InBlock.gifsettings.Compiled
=Compiled.Checked;
InBlock.gifsettings.LeftPanelWidth
=LeftPanel.Width;
InBlock.gif
if(IsVisible)settings.TreeHeight=Tree.Height;
InBlock.gif
elsesettings.TreeHeight=SaveTreeHeight;
InBlock.gifsettings.Size
=this.Size;
InBlock.gif
if(Savior.SaveToFile(settings,ProjectFileName))returntrue;
InBlock.gif
elsereturnfalse;
ExpandedBlockEnd.gif}

None.gif
(5)从设置文件中读取信息
None.gifprivateboolReadFileData()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifsettings
=(Settings)Savior.ReadFromFile(settings,ProjectFileName);
InBlock.gif
if(settings==null)returnfalse;
InBlock.gifInputBox.Text
=settings.InputData;
InBlock.gifReplace.Text
=settings.ReplacementString;
InBlock.gifRegexBox.Text
=settings.RegularExpression;
InBlock.gifMakeForm.FileName
=settings.FileName;
InBlock.gifMakeForm.IsPublic
=settings.IsPublic;
InBlock.gifMakeForm.Namespace
=settings.Namespace;
InBlock.gifMakeForm.ClassName
=settings.ClassName;
InBlock.gifIgnoreCase.Checked
=settings.IgnoreCase;
InBlock.gifMultiline.Checked
=settings.Multiline;
InBlock.gifSingleline.Checked
=settings.Singleline;
InBlock.gifExplicit.Checked
=settings.ExplicitCapture;
InBlock.gifECMA.Checked
=settings.ECMAScript;
InBlock.gifRightToLeftBox.Checked
=settings.RightToLeft;
InBlock.gifIgnorePattern.Checked
=settings.IgnorePatternWS;
InBlock.gifCompiled.Checked
=settings.Compiled;
InBlock.gifLeftPanel.Width
=settings.LeftPanelWidth;
InBlock.gifTree.Height
=settings.TreeHeight;
InBlock.gifSaveTreeHeight
=Tree.Height;
InBlock.gif
this.Size=settings.Size;
InBlock.gif
returntrue;
ExpandedBlockEnd.gif}
(6)树结点选择
None.gifprivatevoidTree_AfterSelect(objectsender,System.Windows.Forms.TreeViewEventArgse)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(Skip)return;
InBlock.gif
if(Tree.SelectedNode.Parent==null)//Mustbethetoplevelnode
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMatchm
=(Match)Tree.SelectedNode.Tag;
InBlock.gifResultsBox.Text
=m.Value;
InBlock.gifstatusBar.Panels[
1].Text="Position:"+m.Index;
InBlock.gifstatusBar.Panels[
2].Text="Length:"+m.Length;
InBlock.gif
if(m!=null&&!Dirty)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifInputBox.Select(m.Index,m.Length);
InBlock.gifInputBox.ScrollToCaret();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(Tree.SelectedNode.Parent.Parent==null)//Mustbeagroup
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifGroupg
=(Group)Tree.SelectedNode.Tag;
InBlock.gifResultsBox.Text
=g.Value;
InBlock.gifstatusBar.Panels[
1].Text="Position:"+g.Index;
InBlock.gifstatusBar.Panels[
2].Text="Length:"+g.Length;
InBlock.gif
if(g!=null&&!Dirty)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifInputBox.Select(g.Index,g.Length);
InBlock.gifInputBox.ScrollToCaret();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else//Mustbeacapture
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifCapturec
=(Capture)Tree.SelectedNode.Tag;
InBlock.gifResultsBox.Text
=c.Value;
InBlock.gifstatusBar.Panels[
1].Text="Position:"+c.Index;
InBlock.gifstatusBar.Panels[
2].Text="Length:"+c.Length;
InBlock.gif
if(c!=null&&!Dirty)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifInputBox.Select(c.Index,c.Length);
InBlock.gifInputBox.ScrollToCaret();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

自动构建正则表达式我也见识过一个这样的工具,不过我对其实现机制还是不大理解,慢慢再说吧,就写这里了,太累了,。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值