不多说废话了,直接贴一些重要的代码:
(1)查找匹配项:
private void RunMatch()
{
Regex r;
Match m;
statusBar.Panels[0].Text="";
statusBar.Panels[1].Text="";
statusBar.Panels[2].Text="";
InputBox.Select(0,0); // Unselect all the text
Dirty=false;
Skip=true;
this.Cursor=Cursors.WaitCursor;
if((r=MakeRegex())==null)
{
this.Cursor=Cursors.Default;
return;
}
Tree.Nodes.Clear();
ResultsBox.Text="";
ShowBuilder(false);
ShowTree(true);
this.Cursor=Cursors.Default;
// Store the results in the text box
for (m = r.Match(InputBox.Text); m.Success; m = m.NextMatch())
{
if(m.Value.Length>0)
{
Tree.Nodes.Add("["+m.Value + "]");
int ThisNode=Tree.Nodes.Count-1;
Tree.Nodes[ThisNode].Tag=m;
if(m.Groups.Count>1)
{
for (int i=1;i<m.Groups.Count;i++)
{
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++)
{
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()
{
Regex r;
this.Cursor=Cursors.WaitCursor;
statusBar.Panels[0].Text="";
statusBar.Panels[1].Text="";
statusBar.Panels[2].Text="";
InputBox.Select(0,0); // Unselect all the text
Dirty=false;
Skip=true;
if((r=MakeRegex())==null)
{
this.Cursor=Cursors.Default;
return;
}
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
{
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);
}
public class RegistrySettings
{
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()
{
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;
}
(5)从设置文件中读取信息
private bool ReadFileData()
{
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)
{
if(Skip)return;
if(Tree.SelectedNode.Parent==null) // Must be the top level node
{
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)
{
InputBox.Select(m.Index,m.Length);
InputBox.ScrollToCaret();
}
}
else if(Tree.SelectedNode.Parent.Parent==null) // Must be a group
{
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)
{
InputBox.Select(g.Index,g.Length);
InputBox.ScrollToCaret();
}
}
else // Must be a capture
{
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)
{
InputBox.Select(c.Index,c.Length);
InputBox.ScrollToCaret();
}
}
}
自动构建正则表达式我也见识过一个这样的工具,不过我对其实现机制还是不大理解,慢慢再说吧,就写这里了,太累了,。。。
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/08/01/465331.html,如需转载请自行联系原作者