C#做的一個正則表達式測試軟件,照書上做的.
源代碼:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace RegexTester
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class RegexTesterForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox InputTextBox;
private System.Windows.Forms.TextBox ReplacementTextBox;
private System.Windows.Forms.TextBox ResultsTextBox;
private System.Windows.Forms.Button TestRegexButton;
private System.Windows.Forms.Button ReplaceButton;
private System.Windows.Forms.Button SplitButton;
private System.Windows.Forms.Button MatchesButton;
private System.Windows.Forms.Button OpenRegexButton;
private System.Windows.Forms.Button SaveRegexButton;
private System.Windows.Forms.GroupBox OptionsGroup;
private System.Windows.Forms.CheckBox SingleLineChkBox;
private System.Windows.Forms.CheckBox RightToLeftChkBox;
private System.Windows.Forms.CheckBox MultiLineChkBox;
private System.Windows.Forms.CheckBox IgnorePatternWhiteSpaceChkBox;
private System.Windows.Forms.CheckBox IgnoreCaseChkBox;
private System.Windows.Forms.CheckBox ExplicitCaptureChkBox;
private System.Windows.Forms.CheckBox ECMAScriptChkBox;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox RegexTextBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public RegexTesterForm()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.RegexTextBox = new System.Windows.Forms.TextBox();
this.InputTextBox = new System.Windows.Forms.TextBox();
this.ReplacementTextBox = new System.Windows.Forms.TextBox();
this.ResultsTextBox = new System.Windows.Forms.TextBox();
this.TestRegexButton = new System.Windows.Forms.Button();
this.ReplaceButton = new System.Windows.Forms.Button();
this.SplitButton = new System.Windows.Forms.Button();
this.MatchesButton = new System.Windows.Forms.Button();
this.OpenRegexButton = new System.Windows.Forms.Button();
this.SaveRegexButton = new System.Windows.Forms.Button();
this.OptionsGroup = new System.Windows.Forms.GroupBox();
this.ECMAScriptChkBox = new System.Windows.Forms.CheckBox();
this.ExplicitCaptureChkBox = new System.Windows.Forms.CheckBox();
this.IgnoreCaseChkBox = new System.Windows.Forms.CheckBox();
this.IgnorePatternWhiteSpaceChkBox = new System.Windows.Forms.CheckBox();
this.MultiLineChkBox = new System.Windows.Forms.CheckBox();
this.RightToLeftChkBox = new System.Windows.Forms.CheckBox();
this.SingleLineChkBox = new System.Windows.Forms.CheckBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.OptionsGroup.SuspendLayout();
this.SuspendLayout();
//
// RegexTextBox
//
this.RegexTextBox.Location = new System.Drawing.Point(8, 16);
this.RegexTextBox.Multiline = true;
this.RegexTextBox.Name = "RegexTextBox";
this.RegexTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.RegexTextBox.Size = new System.Drawing.Size(432, 56);
this.RegexTextBox.TabIndex = 0;
this.RegexTextBox.Text = "";
//
// InputTextBox
//
this.InputTextBox.Location = new System.Drawing.Point(8, 176);
this.InputTextBox.Multiline = true;
this.InputTextBox.Name = "InputTextBox";
this.InputTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.InputTextBox.Size = new System.Drawing.Size(432, 88);
this.InputTextBox.TabIndex = 1;
this.InputTextBox.Text = "";
//
// ReplacementTextBox
//
this.ReplacementTextBox.Location = new System.Drawing.Point(8, 280);
this.ReplacementTextBox.Multiline = true;
this.ReplacementTextBox.Name = "ReplacementTextBox";
this.ReplacementTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.ReplacementTextBox.Size = new System.Drawing.Size(432, 64);
this.ReplacementTextBox.TabIndex = 2;
this.ReplacementTextBox.Text = "";
//
// ResultsTextBox
//
this.ResultsTextBox.Location = new System.Drawing.Point(8, 360);
this.ResultsTextBox.Multiline = true;
this.ResultsTextBox.Name = "ResultsTextBox";
this.ResultsTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.ResultsTextBox.Size = new System.Drawing.Size(432, 72);
this.ResultsTextBox.TabIndex = 3;
this.ResultsTextBox.Text = "";
//
// TestRegexButton
//
this.TestRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.TestRegexButton.Location = new System.Drawing.Point(32, 440);
this.TestRegexButton.Name = "TestRegexButton";
this.TestRegexButton.Size = new System.Drawing.Size(74, 23);
this.TestRegexButton.TabIndex = 4;
this.TestRegexButton.Text = "IsMatch()";
this.TestRegexButton.Click += new System.EventHandler(this.TestRegexButton_Click);
//
// ReplaceButton
//
this.ReplaceButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.ReplaceButton.Location = new System.Drawing.Point(128, 440);
this.ReplaceButton.Name = "ReplaceButton";
this.ReplaceButton.Size = new System.Drawing.Size(74, 23);
this.ReplaceButton.TabIndex = 5;
this.ReplaceButton.Text = "Replace()";
this.ReplaceButton.Click += new System.EventHandler(this.ReplaceButton_Click);
//
// SplitButton
//
this.SplitButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.SplitButton.Location = new System.Drawing.Point(232, 440);
this.SplitButton.Name = "SplitButton";
this.SplitButton.TabIndex = 6;
this.SplitButton.Text = "Split()";
this.SplitButton.Click += new System.EventHandler(this.SplitButton_Click);
//
// MatchesButton
//
this.MatchesButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.MatchesButton.Location = new System.Drawing.Point(336, 440);
this.MatchesButton.Name = "MatchesButton";
this.MatchesButton.TabIndex = 7;
this.MatchesButton.Text = "Matches()";
this.MatchesButton.Click += new System.EventHandler(this.MatchesButton_Click);
//
// OpenRegexButton
//
this.OpenRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.OpenRegexButton.Location = new System.Drawing.Point(360, 88);
this.OpenRegexButton.Name = "OpenRegexButton";
this.OpenRegexButton.Size = new System.Drawing.Size(80, 23);
this.OpenRegexButton.TabIndex = 8;
this.OpenRegexButton.Text = "Open Regex";
this.OpenRegexButton.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.OpenRegexButton.Click += new System.EventHandler(this.OpenRegexButton_Click);
//
// SaveRegexButton
//
this.SaveRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.SaveRegexButton.Location = new System.Drawing.Point(360, 128);
this.SaveRegexButton.Name = "SaveRegexButton";
this.SaveRegexButton.Size = new System.Drawing.Size(80, 23);
this.SaveRegexButton.TabIndex = 9;
this.SaveRegexButton.Text = "Save Regex";
this.SaveRegexButton.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.SaveRegexButton.Click += new System.EventHandler(this.SaveRegexButton_Click);
//
// OptionsGroup
//
this.OptionsGroup.Controls.Add(this.ECMAScriptChkBox);
this.OptionsGroup.Controls.Add(this.ExplicitCaptureChkBox);
this.OptionsGroup.Controls.Add(this.IgnoreCaseChkBox);
this.OptionsGroup.Controls.Add(this.IgnorePatternWhiteSpaceChkBox);
this.OptionsGroup.Controls.Add(this.MultiLineChkBox);
this.OptionsGroup.Controls.Add(this.RightToLeftChkBox);
this.OptionsGroup.Controls.Add(this.SingleLineChkBox);
this.OptionsGroup.Location = new System.Drawing.Point(8, 80);
this.OptionsGroup.Name = "OptionsGroup";
this.OptionsGroup.Size = new System.Drawing.Size(344, 72);
this.OptionsGroup.TabIndex = 10;
this.OptionsGroup.TabStop = false;
this.OptionsGroup.Text = "Regex Options";
//
// ECMAScriptChkBox
//
this.ECMAScriptChkBox.Location = new System.Drawing.Point(16, 32);
this.ECMAScriptChkBox.Name = "ECMAScriptChkBox";
this.ECMAScriptChkBox.Size = new System.Drawing.Size(88, 16);
this.ECMAScriptChkBox.TabIndex = 7;
this.ECMAScriptChkBox.Text = "ECMAScript";
//
// ExplicitCaptureChkBox
//
this.ExplicitCaptureChkBox.Location = new System.Drawing.Point(216, 32);
this.ExplicitCaptureChkBox.Name = "ExplicitCaptureChkBox";
this.ExplicitCaptureChkBox.Size = new System.Drawing.Size(120, 16);
this.ExplicitCaptureChkBox.TabIndex = 5;
this.ExplicitCaptureChkBox.Text = "ExplicitCapture";
//
// IgnoreCaseChkBox
//
this.IgnoreCaseChkBox.Location = new System.Drawing.Point(112, 32);
this.IgnoreCaseChkBox.Name = "IgnoreCaseChkBox";
this.IgnoreCaseChkBox.Size = new System.Drawing.Size(96, 16);
this.IgnoreCaseChkBox.TabIndex = 4;
this.IgnoreCaseChkBox.Text = "IgnoreCase";
//
// IgnorePatternWhiteSpaceChkBox
//
this.IgnorePatternWhiteSpaceChkBox.Location = new System.Drawing.Point(16, 48);
this.IgnorePatternWhiteSpaceChkBox.Name = "IgnorePatternWhiteSpaceChkBox";
this.IgnorePatternWhiteSpaceChkBox.Size = new System.Drawing.Size(176, 16);
this.IgnorePatternWhiteSpaceChkBox.TabIndex = 3;
this.IgnorePatternWhiteSpaceChkBox.Text = "IgnorePatternWhiteSpace";
//
// MultiLineChkBox
//
this.MultiLineChkBox.Location = new System.Drawing.Point(216, 16);
this.MultiLineChkBox.Name = "MultiLineChkBox";
this.MultiLineChkBox.Size = new System.Drawing.Size(80, 16);
this.MultiLineChkBox.TabIndex = 2;
this.MultiLineChkBox.Text = "MultiLine";
//
// RightToLeftChkBox
//
this.RightToLeftChkBox.Location = new System.Drawing.Point(112, 16);
this.RightToLeftChkBox.Name = "RightToLeftChkBox";
this.RightToLeftChkBox.Size = new System.Drawing.Size(96, 16);
this.RightToLeftChkBox.TabIndex = 1;
this.RightToLeftChkBox.Text = "RightToLeft";
//
// SingleLineChkBox
//
this.SingleLineChkBox.Location = new System.Drawing.Point(16, 16);
this.SingleLineChkBox.Name = "SingleLineChkBox";
this.SingleLineChkBox.Size = new System.Drawing.Size(88, 16);
this.SingleLineChkBox.TabIndex = 0;
this.SingleLineChkBox.Text = "SingleLine";
//
// openFileDialog1
//
this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
// saveFileDialog1
//
this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 160);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 11;
this.label1.Text = "Text to Match On";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 264);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(112, 16);
this.label2.TabIndex = 12;
this.label2.Text = "Replacement Text";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 344);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 16);
this.label3.TabIndex = 13;
this.label3.Text = "Results of Match";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(200, 16);
this.label4.TabIndex = 14;
this.label4.Text = "Regular Expression";
//
// RegexTesterForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 469);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.OptionsGroup);
this.Controls.Add(this.SaveRegexButton);
this.Controls.Add(this.OpenRegexButton);
this.Controls.Add(this.MatchesButton);
this.Controls.Add(this.SplitButton);
this.Controls.Add(this.ReplaceButton);
this.Controls.Add(this.TestRegexButton);
this.Controls.Add(this.ResultsTextBox);
this.Controls.Add(this.ReplacementTextBox);
this.Controls.Add(this.InputTextBox);
this.Controls.Add(this.RegexTextBox);
this.Name = "RegexTesterForm";
this.Text = "RegexTester";
this.OptionsGroup.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new RegexTesterForm());
}
private void SaveRegexButton_Click(object sender, System.EventArgs e)
{
this.saveFileDialog1.ShowDialog();
}
private void OpenRegexButton_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
StreamWriter steamWriterRegex=File.CreateText(saveFileDialog1.FileName);
steamWriterRegex.Write(this.RegexTextBox.Text);
steamWriterRegex.Close();
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
StreamReader steamReaderRegex=File.OpenText(openFileDialog1.FileName);
this.RegexTextBox.Text=steamReaderRegex.ReadToEnd();
steamReaderRegex.Close();
}
private RegexOptions GetSelectedRegexOptions()
{
RegexOptions selectedRegexOptions=RegexOptions.None;
if(this.IgnoreCaseChkBox.Checked)
selectedRegexOptions|=RegexOptions.IgnoreCase;
if(this.ExplicitCaptureChkBox.Checked)
selectedRegexOptions|=RegexOptions.ExplicitCapture;
if(this.ECMAScriptChkBox.Checked)
selectedRegexOptions|=RegexOptions.ECMAScript;
if(this.IgnorePatternWhiteSpaceChkBox.Checked)
selectedRegexOptions|=RegexOptions.IgnorePatternWhitespace;
if(this.MultiLineChkBox.Checked)
selectedRegexOptions|=RegexOptions.Multiline;
if(this.RightToLeftChkBox.Checked)
selectedRegexOptions|=RegexOptions.RightToLeft;
if(this.SingleLineChkBox.Checked)
selectedRegexOptions|=RegexOptions.Singleline;
return selectedRegexOptions;
}
private void TestRegexButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selctedRegexOptins=this.GetSelectedRegexOptions();
Regex testRegex=new Regex(this.RegexTextBox.Text,selctedRegexOptins);
if(testRegex.IsMatch(this.InputTextBox.Text))
{
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text="MATCH FOUND";
}
else
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="NO MATCH FOUND";
}
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
private void ReplaceButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
Regex replaceRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=replaceRegex.Replace(this.InputTextBox.Text,this.ReplacementTextBox.Text);
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
private void SplitButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
Regex splitRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
string[] splitResults;
splitResults=splitRegex.Split(this.InputTextBox.Text);
StringBuilder resultsString =new StringBuilder(this.InputTextBox.Text.Length);
foreach(string stringElement in splitResults)
{
resultsString.Append(stringElement+Environment.NewLine);
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
private void MatchesButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
Regex matchesRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
MatchCollection matchesFound;
matchesFound=matchesRegex.Matches(this.InputTextBox.Text);
string nextMatch="----------Next Match------------ ";
StringBuilder resultsString =new StringBuilder(64);
foreach(Match matchMade in matchesFound)
{
resultsString.Append(matchMade.Value+(Environment.NewLine+nextMatch));
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace RegexTester
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class RegexTesterForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox InputTextBox;
private System.Windows.Forms.TextBox ReplacementTextBox;
private System.Windows.Forms.TextBox ResultsTextBox;
private System.Windows.Forms.Button TestRegexButton;
private System.Windows.Forms.Button ReplaceButton;
private System.Windows.Forms.Button SplitButton;
private System.Windows.Forms.Button MatchesButton;
private System.Windows.Forms.Button OpenRegexButton;
private System.Windows.Forms.Button SaveRegexButton;
private System.Windows.Forms.GroupBox OptionsGroup;
private System.Windows.Forms.CheckBox SingleLineChkBox;
private System.Windows.Forms.CheckBox RightToLeftChkBox;
private System.Windows.Forms.CheckBox MultiLineChkBox;
private System.Windows.Forms.CheckBox IgnorePatternWhiteSpaceChkBox;
private System.Windows.Forms.CheckBox IgnoreCaseChkBox;
private System.Windows.Forms.CheckBox ExplicitCaptureChkBox;
private System.Windows.Forms.CheckBox ECMAScriptChkBox;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.TextBox RegexTextBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public RegexTesterForm()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.RegexTextBox = new System.Windows.Forms.TextBox();
this.InputTextBox = new System.Windows.Forms.TextBox();
this.ReplacementTextBox = new System.Windows.Forms.TextBox();
this.ResultsTextBox = new System.Windows.Forms.TextBox();
this.TestRegexButton = new System.Windows.Forms.Button();
this.ReplaceButton = new System.Windows.Forms.Button();
this.SplitButton = new System.Windows.Forms.Button();
this.MatchesButton = new System.Windows.Forms.Button();
this.OpenRegexButton = new System.Windows.Forms.Button();
this.SaveRegexButton = new System.Windows.Forms.Button();
this.OptionsGroup = new System.Windows.Forms.GroupBox();
this.ECMAScriptChkBox = new System.Windows.Forms.CheckBox();
this.ExplicitCaptureChkBox = new System.Windows.Forms.CheckBox();
this.IgnoreCaseChkBox = new System.Windows.Forms.CheckBox();
this.IgnorePatternWhiteSpaceChkBox = new System.Windows.Forms.CheckBox();
this.MultiLineChkBox = new System.Windows.Forms.CheckBox();
this.RightToLeftChkBox = new System.Windows.Forms.CheckBox();
this.SingleLineChkBox = new System.Windows.Forms.CheckBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.OptionsGroup.SuspendLayout();
this.SuspendLayout();
//
// RegexTextBox
//
this.RegexTextBox.Location = new System.Drawing.Point(8, 16);
this.RegexTextBox.Multiline = true;
this.RegexTextBox.Name = "RegexTextBox";
this.RegexTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.RegexTextBox.Size = new System.Drawing.Size(432, 56);
this.RegexTextBox.TabIndex = 0;
this.RegexTextBox.Text = "";
//
// InputTextBox
//
this.InputTextBox.Location = new System.Drawing.Point(8, 176);
this.InputTextBox.Multiline = true;
this.InputTextBox.Name = "InputTextBox";
this.InputTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.InputTextBox.Size = new System.Drawing.Size(432, 88);
this.InputTextBox.TabIndex = 1;
this.InputTextBox.Text = "";
//
// ReplacementTextBox
//
this.ReplacementTextBox.Location = new System.Drawing.Point(8, 280);
this.ReplacementTextBox.Multiline = true;
this.ReplacementTextBox.Name = "ReplacementTextBox";
this.ReplacementTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.ReplacementTextBox.Size = new System.Drawing.Size(432, 64);
this.ReplacementTextBox.TabIndex = 2;
this.ReplacementTextBox.Text = "";
//
// ResultsTextBox
//
this.ResultsTextBox.Location = new System.Drawing.Point(8, 360);
this.ResultsTextBox.Multiline = true;
this.ResultsTextBox.Name = "ResultsTextBox";
this.ResultsTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.ResultsTextBox.Size = new System.Drawing.Size(432, 72);
this.ResultsTextBox.TabIndex = 3;
this.ResultsTextBox.Text = "";
//
// TestRegexButton
//
this.TestRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.TestRegexButton.Location = new System.Drawing.Point(32, 440);
this.TestRegexButton.Name = "TestRegexButton";
this.TestRegexButton.Size = new System.Drawing.Size(74, 23);
this.TestRegexButton.TabIndex = 4;
this.TestRegexButton.Text = "IsMatch()";
this.TestRegexButton.Click += new System.EventHandler(this.TestRegexButton_Click);
//
// ReplaceButton
//
this.ReplaceButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.ReplaceButton.Location = new System.Drawing.Point(128, 440);
this.ReplaceButton.Name = "ReplaceButton";
this.ReplaceButton.Size = new System.Drawing.Size(74, 23);
this.ReplaceButton.TabIndex = 5;
this.ReplaceButton.Text = "Replace()";
this.ReplaceButton.Click += new System.EventHandler(this.ReplaceButton_Click);
//
// SplitButton
//
this.SplitButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.SplitButton.Location = new System.Drawing.Point(232, 440);
this.SplitButton.Name = "SplitButton";
this.SplitButton.TabIndex = 6;
this.SplitButton.Text = "Split()";
this.SplitButton.Click += new System.EventHandler(this.SplitButton_Click);
//
// MatchesButton
//
this.MatchesButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.MatchesButton.Location = new System.Drawing.Point(336, 440);
this.MatchesButton.Name = "MatchesButton";
this.MatchesButton.TabIndex = 7;
this.MatchesButton.Text = "Matches()";
this.MatchesButton.Click += new System.EventHandler(this.MatchesButton_Click);
//
// OpenRegexButton
//
this.OpenRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.OpenRegexButton.Location = new System.Drawing.Point(360, 88);
this.OpenRegexButton.Name = "OpenRegexButton";
this.OpenRegexButton.Size = new System.Drawing.Size(80, 23);
this.OpenRegexButton.TabIndex = 8;
this.OpenRegexButton.Text = "Open Regex";
this.OpenRegexButton.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.OpenRegexButton.Click += new System.EventHandler(this.OpenRegexButton_Click);
//
// SaveRegexButton
//
this.SaveRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.SaveRegexButton.Location = new System.Drawing.Point(360, 128);
this.SaveRegexButton.Name = "SaveRegexButton";
this.SaveRegexButton.Size = new System.Drawing.Size(80, 23);
this.SaveRegexButton.TabIndex = 9;
this.SaveRegexButton.Text = "Save Regex";
this.SaveRegexButton.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.SaveRegexButton.Click += new System.EventHandler(this.SaveRegexButton_Click);
//
// OptionsGroup
//
this.OptionsGroup.Controls.Add(this.ECMAScriptChkBox);
this.OptionsGroup.Controls.Add(this.ExplicitCaptureChkBox);
this.OptionsGroup.Controls.Add(this.IgnoreCaseChkBox);
this.OptionsGroup.Controls.Add(this.IgnorePatternWhiteSpaceChkBox);
this.OptionsGroup.Controls.Add(this.MultiLineChkBox);
this.OptionsGroup.Controls.Add(this.RightToLeftChkBox);
this.OptionsGroup.Controls.Add(this.SingleLineChkBox);
this.OptionsGroup.Location = new System.Drawing.Point(8, 80);
this.OptionsGroup.Name = "OptionsGroup";
this.OptionsGroup.Size = new System.Drawing.Size(344, 72);
this.OptionsGroup.TabIndex = 10;
this.OptionsGroup.TabStop = false;
this.OptionsGroup.Text = "Regex Options";
//
// ECMAScriptChkBox
//
this.ECMAScriptChkBox.Location = new System.Drawing.Point(16, 32);
this.ECMAScriptChkBox.Name = "ECMAScriptChkBox";
this.ECMAScriptChkBox.Size = new System.Drawing.Size(88, 16);
this.ECMAScriptChkBox.TabIndex = 7;
this.ECMAScriptChkBox.Text = "ECMAScript";
//
// ExplicitCaptureChkBox
//
this.ExplicitCaptureChkBox.Location = new System.Drawing.Point(216, 32);
this.ExplicitCaptureChkBox.Name = "ExplicitCaptureChkBox";
this.ExplicitCaptureChkBox.Size = new System.Drawing.Size(120, 16);
this.ExplicitCaptureChkBox.TabIndex = 5;
this.ExplicitCaptureChkBox.Text = "ExplicitCapture";
//
// IgnoreCaseChkBox
//
this.IgnoreCaseChkBox.Location = new System.Drawing.Point(112, 32);
this.IgnoreCaseChkBox.Name = "IgnoreCaseChkBox";
this.IgnoreCaseChkBox.Size = new System.Drawing.Size(96, 16);
this.IgnoreCaseChkBox.TabIndex = 4;
this.IgnoreCaseChkBox.Text = "IgnoreCase";
//
// IgnorePatternWhiteSpaceChkBox
//
this.IgnorePatternWhiteSpaceChkBox.Location = new System.Drawing.Point(16, 48);
this.IgnorePatternWhiteSpaceChkBox.Name = "IgnorePatternWhiteSpaceChkBox";
this.IgnorePatternWhiteSpaceChkBox.Size = new System.Drawing.Size(176, 16);
this.IgnorePatternWhiteSpaceChkBox.TabIndex = 3;
this.IgnorePatternWhiteSpaceChkBox.Text = "IgnorePatternWhiteSpace";
//
// MultiLineChkBox
//
this.MultiLineChkBox.Location = new System.Drawing.Point(216, 16);
this.MultiLineChkBox.Name = "MultiLineChkBox";
this.MultiLineChkBox.Size = new System.Drawing.Size(80, 16);
this.MultiLineChkBox.TabIndex = 2;
this.MultiLineChkBox.Text = "MultiLine";
//
// RightToLeftChkBox
//
this.RightToLeftChkBox.Location = new System.Drawing.Point(112, 16);
this.RightToLeftChkBox.Name = "RightToLeftChkBox";
this.RightToLeftChkBox.Size = new System.Drawing.Size(96, 16);
this.RightToLeftChkBox.TabIndex = 1;
this.RightToLeftChkBox.Text = "RightToLeft";
//
// SingleLineChkBox
//
this.SingleLineChkBox.Location = new System.Drawing.Point(16, 16);
this.SingleLineChkBox.Name = "SingleLineChkBox";
this.SingleLineChkBox.Size = new System.Drawing.Size(88, 16);
this.SingleLineChkBox.TabIndex = 0;
this.SingleLineChkBox.Text = "SingleLine";
//
// openFileDialog1
//
this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
// saveFileDialog1
//
this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 160);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 11;
this.label1.Text = "Text to Match On";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 264);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(112, 16);
this.label2.TabIndex = 12;
this.label2.Text = "Replacement Text";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 344);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 16);
this.label3.TabIndex = 13;
this.label3.Text = "Results of Match";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(200, 16);
this.label4.TabIndex = 14;
this.label4.Text = "Regular Expression";
//
// RegexTesterForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 469);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.OptionsGroup);
this.Controls.Add(this.SaveRegexButton);
this.Controls.Add(this.OpenRegexButton);
this.Controls.Add(this.MatchesButton);
this.Controls.Add(this.SplitButton);
this.Controls.Add(this.ReplaceButton);
this.Controls.Add(this.TestRegexButton);
this.Controls.Add(this.ResultsTextBox);
this.Controls.Add(this.ReplacementTextBox);
this.Controls.Add(this.InputTextBox);
this.Controls.Add(this.RegexTextBox);
this.Name = "RegexTesterForm";
this.Text = "RegexTester";
this.OptionsGroup.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new RegexTesterForm());
}
private void SaveRegexButton_Click(object sender, System.EventArgs e)
{
this.saveFileDialog1.ShowDialog();
}
private void OpenRegexButton_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
StreamWriter steamWriterRegex=File.CreateText(saveFileDialog1.FileName);
steamWriterRegex.Write(this.RegexTextBox.Text);
steamWriterRegex.Close();
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
StreamReader steamReaderRegex=File.OpenText(openFileDialog1.FileName);
this.RegexTextBox.Text=steamReaderRegex.ReadToEnd();
steamReaderRegex.Close();
}
private RegexOptions GetSelectedRegexOptions()
{
RegexOptions selectedRegexOptions=RegexOptions.None;
if(this.IgnoreCaseChkBox.Checked)
selectedRegexOptions|=RegexOptions.IgnoreCase;
if(this.ExplicitCaptureChkBox.Checked)
selectedRegexOptions|=RegexOptions.ExplicitCapture;
if(this.ECMAScriptChkBox.Checked)
selectedRegexOptions|=RegexOptions.ECMAScript;
if(this.IgnorePatternWhiteSpaceChkBox.Checked)
selectedRegexOptions|=RegexOptions.IgnorePatternWhitespace;
if(this.MultiLineChkBox.Checked)
selectedRegexOptions|=RegexOptions.Multiline;
if(this.RightToLeftChkBox.Checked)
selectedRegexOptions|=RegexOptions.RightToLeft;
if(this.SingleLineChkBox.Checked)
selectedRegexOptions|=RegexOptions.Singleline;
return selectedRegexOptions;
}
private void TestRegexButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selctedRegexOptins=this.GetSelectedRegexOptions();
Regex testRegex=new Regex(this.RegexTextBox.Text,selctedRegexOptins);
if(testRegex.IsMatch(this.InputTextBox.Text))
{
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text="MATCH FOUND";
}
else
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="NO MATCH FOUND";
}
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
private void ReplaceButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
Regex replaceRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=replaceRegex.Replace(this.InputTextBox.Text,this.ReplacementTextBox.Text);
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
private void SplitButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
Regex splitRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
string[] splitResults;
splitResults=splitRegex.Split(this.InputTextBox.Text);
StringBuilder resultsString =new StringBuilder(this.InputTextBox.Text.Length);
foreach(string stringElement in splitResults)
{
resultsString.Append(stringElement+Environment.NewLine);
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
private void MatchesButton_Click(object sender, System.EventArgs e)
{
try
{
RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
Regex matchesRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
MatchCollection matchesFound;
matchesFound=matchesRegex.Matches(this.InputTextBox.Text);
string nextMatch="----------Next Match------------ ";
StringBuilder resultsString =new StringBuilder(64);
foreach(Match matchMade in matchesFound)
{
resultsString.Append(matchMade.Value+(Environment.NewLine+nextMatch));
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
}
catch(ArgumentException ex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="There was a error in your regular expressions: "+ex.ToString();
}
}
}
}