![](/images/cnblogs_com/phinecos/200673101.JPG)
不多说废话了,直接贴一些重要的代码:
(1)查找匹配项:
private void RunMatch()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Regex r;
Match m;
![](/Images/OutliningIndicators/InBlock.gif)
statusBar.Panels[0].Text="";
statusBar.Panels[1].Text="";
statusBar.Panels[2].Text="";
![](/Images/OutliningIndicators/InBlock.gif)
InputBox.Select(0,0); // Unselect all the text
Dirty=false;
Skip=true;
![](/Images/OutliningIndicators/InBlock.gif)
this.Cursor=Cursors.WaitCursor;
if((r=MakeRegex())==null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor=Cursors.Default;
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
Tree.Nodes.Clear();
ResultsBox.Text="";
ShowBuilder(false);
ShowTree(true);
this.Cursor=Cursors.Default;
![](/Images/OutliningIndicators/InBlock.gif)
// Store the results in the text box
for (m = r.Match(InputBox.Text); m.Success; m = m.NextMatch())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(m.Value.Length>0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tree.Nodes.Add("["+m.Value + "]");
int ThisNode=Tree.Nodes.Count-1;
Tree.Nodes[ThisNode].Tag=m;
if(m.Groups.Count>1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i=1;i<m.Groups.Count;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tree.Nodes[ThisNode].Nodes.Add(r.GroupNameFromNumber(i)+": ["+m.Groups[i].Value+"]");
Tree.Nodes[ThisNode].Nodes[i-1].Tag=m.Groups[i];
//This bit of code puts in another level of nodes showing the captures for each group
int Number=m.Groups[i].Captures.Count;
if(Number>1)
for(int j=0;j<Number;j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tree.Nodes[ThisNode].Nodes[i-1].Nodes.Add(m.Groups[i].Captures[j].Value);
Tree.Nodes[ThisNode].Nodes[i-1].Nodes[j].Tag=m.Groups[i].Captures[j];
}
}
}
}
}
statusBar.Panels[0].Text=Tree.Nodes.Count.ToString()+" Matches";
Skip=false;
}
(2)替换匹配项
private void RunReplace()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Regex r;
this.Cursor=Cursors.WaitCursor;
statusBar.Panels[0].Text="";
statusBar.Panels[1].Text="";
statusBar.Panels[2].Text="";
![](/Images/OutliningIndicators/InBlock.gif)
InputBox.Select(0,0); // Unselect all the text
Dirty=false;
Skip=true;
![](/Images/OutliningIndicators/InBlock.gif)
if((r=MakeRegex())==null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor=Cursors.Default;
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
Tree.Nodes.Clear();
ShowBuilder(false);
ShowTree(false);
this.Cursor=Cursors.Default;
ResultsBox.Text=r.Replace(InputBox.Text,Replace.Text);
Skip=false;
statusBar.Panels[0].Text="";
}
(3)保存项目信息的两个类
[Serializable]
public class Settings
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public string FileName="TheRegexAssembly.dll";
public string ClassName="TheRegexClass";
public string Namespace="TheRegex";
public bool IsPublic=true;
public string InputData= "";
public string RegularExpression = "";
public string ReplacementString = "";
public bool IgnoreCase = true;
public bool Multiline = true;
public bool Singleline = false;
public bool ExplicitCapture = false;
public bool ECMAScript = false;
public bool RightToLeft = false;
public bool IgnorePatternWS = true;
public bool Compiled = true;
public int LeftPanelWidth = 470;
public int TreeHeight = 150;
public Size Size = new Size(800, 400);
}
![](/Images/OutliningIndicators/None.gif)
public class RegistrySettings
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public string[] MRUList = new string[0];
public Point Location = new Point(100,100);
public string ProjectFile ="";
public string OpenPathName="";
public string SavePathName="";
}
(4)保存项目
private bool SaveFileData()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
settings.InputData=InputBox.Text;
settings.IsPublic=MakeForm.IsPublic;
settings.Namespace=MakeForm.Namespace;
settings.ClassName=MakeForm.ClassName;
settings.InputData=InputBox.Text;
settings.ReplacementString=Replace.Text;
settings.RegularExpression=RegexBox.Text;
settings.IgnoreCase=IgnoreCase.Checked;
settings.Multiline=Multiline.Checked;
settings.Singleline=Singleline.Checked;
settings.ExplicitCapture=Explicit.Checked;
settings.ECMAScript=ECMA.Checked;
settings.RightToLeft=RightToLeftBox.Checked;
settings.IgnorePatternWS=IgnorePattern.Checked;
settings.Compiled=Compiled.Checked;
settings.LeftPanelWidth=LeftPanel.Width;
if(IsVisible)settings.TreeHeight=Tree.Height;
else settings.TreeHeight=SaveTreeHeight;
settings.Size=this.Size;
if(Savior.SaveToFile(settings, ProjectFileName))return true;
else return false;
}
![](/Images/OutliningIndicators/None.gif)
(5)从设置文件中读取信息
private bool ReadFileData()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
settings = (Settings)Savior.ReadFromFile(settings,ProjectFileName);
if(settings==null)return false;
InputBox.Text=settings.InputData;
Replace.Text=settings.ReplacementString;
RegexBox.Text=settings.RegularExpression;
MakeForm.FileName=settings.FileName;
MakeForm.IsPublic=settings.IsPublic;
MakeForm.Namespace=settings.Namespace;
MakeForm.ClassName=settings.ClassName;
IgnoreCase.Checked=settings.IgnoreCase;
Multiline.Checked=settings.Multiline;
Singleline.Checked=settings.Singleline;
Explicit.Checked=settings.ExplicitCapture;
ECMA.Checked=settings.ECMAScript;
RightToLeftBox.Checked=settings.RightToLeft;
IgnorePattern.Checked=settings.IgnorePatternWS;
Compiled.Checked=settings.Compiled;
LeftPanel.Width=settings.LeftPanelWidth;
Tree.Height=settings.TreeHeight;
SaveTreeHeight=Tree.Height;
this.Size=settings.Size;
return true;
}
(6)树结点选择
private void Tree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(Skip)return;
if(Tree.SelectedNode.Parent==null) // Must be the top level node
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Match m=(Match)Tree.SelectedNode.Tag;
ResultsBox.Text=m.Value;
statusBar.Panels[1].Text="Position: "+m.Index;
statusBar.Panels[2].Text="Length: "+m.Length;
if(m!=null && !Dirty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InputBox.Select(m.Index,m.Length);
InputBox.ScrollToCaret();
}
}
else if(Tree.SelectedNode.Parent.Parent==null) // Must be a group
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Group g=(Group)Tree.SelectedNode.Tag;
ResultsBox.Text=g.Value;
statusBar.Panels[1].Text="Position: "+g.Index;
statusBar.Panels[2].Text="Length: "+g.Length;
if(g!=null && !Dirty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InputBox.Select(g.Index,g.Length);
InputBox.ScrollToCaret();
}
}
else // Must be a capture
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Capture c=(Capture)Tree.SelectedNode.Tag;
ResultsBox.Text=c.Value;
statusBar.Panels[1].Text="Position: "+c.Index;
statusBar.Panels[2].Text="Length: "+c.Length;
if(c!=null && !Dirty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InputBox.Select(c.Index,c.Length);
InputBox.ScrollToCaret();
}
}
}
自动构建正则表达式我也见识过一个这样的工具,不过我对其实现机制还是不大理解,慢慢再说吧,就写这里了,太累了,。。。