C#做的一個正則表達式測試軟件,照書上做的.

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(816);
            
this.RegexTextBox.Multiline = true;
            
this.RegexTextBox.Name = "RegexTextBox";
            
this.RegexTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            
this.RegexTextBox.Size = new System.Drawing.Size(43256);
            
this.RegexTextBox.TabIndex = 0;
            
this.RegexTextBox.Text = "";
            
// 
            
// InputTextBox
            
// 
            this.InputTextBox.Location = new System.Drawing.Point(8176);
            
this.InputTextBox.Multiline = true;
            
this.InputTextBox.Name = "InputTextBox";
            
this.InputTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            
this.InputTextBox.Size = new System.Drawing.Size(43288);
            
this.InputTextBox.TabIndex = 1;
            
this.InputTextBox.Text = "";
            
// 
            
// ReplacementTextBox
            
// 
            this.ReplacementTextBox.Location = new System.Drawing.Point(8280);
            
this.ReplacementTextBox.Multiline = true;
            
this.ReplacementTextBox.Name = "ReplacementTextBox";
            
this.ReplacementTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            
this.ReplacementTextBox.Size = new System.Drawing.Size(43264);
            
this.ReplacementTextBox.TabIndex = 2;
            
this.ReplacementTextBox.Text = "";
            
// 
            
// ResultsTextBox
            
// 
            this.ResultsTextBox.Location = new System.Drawing.Point(8360);
            
this.ResultsTextBox.Multiline = true;
            
this.ResultsTextBox.Name = "ResultsTextBox";
            
this.ResultsTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            
this.ResultsTextBox.Size = new System.Drawing.Size(43272);
            
this.ResultsTextBox.TabIndex = 3;
            
this.ResultsTextBox.Text = "";
            
// 
            
// TestRegexButton
            
// 
            this.TestRegexButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            
this.TestRegexButton.Location = new System.Drawing.Point(32440);
            
this.TestRegexButton.Name = "TestRegexButton";
            
this.TestRegexButton.Size = new System.Drawing.Size(7423);
            
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(128440);
            
this.ReplaceButton.Name = "ReplaceButton";
            
this.ReplaceButton.Size = new System.Drawing.Size(7423);
            
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(232440);
            
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(336440);
            
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(36088);
            
this.OpenRegexButton.Name = "OpenRegexButton";
            
this.OpenRegexButton.Size = new System.Drawing.Size(8023);
            
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(360128);
            
this.SaveRegexButton.Name = "SaveRegexButton";
            
this.SaveRegexButton.Size = new System.Drawing.Size(8023);
            
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(880);
            
this.OptionsGroup.Name = "OptionsGroup";
            
this.OptionsGroup.Size = new System.Drawing.Size(34472);
            
this.OptionsGroup.TabIndex = 10;
            
this.OptionsGroup.TabStop = false;
            
this.OptionsGroup.Text = "Regex Options";
            
// 
            
// ECMAScriptChkBox
            
// 
            this.ECMAScriptChkBox.Location = new System.Drawing.Point(1632);
            
this.ECMAScriptChkBox.Name = "ECMAScriptChkBox";
            
this.ECMAScriptChkBox.Size = new System.Drawing.Size(8816);
            
this.ECMAScriptChkBox.TabIndex = 7;
            
this.ECMAScriptChkBox.Text = "ECMAScript";
            
// 
            
// ExplicitCaptureChkBox
            
// 
            this.ExplicitCaptureChkBox.Location = new System.Drawing.Point(21632);
            
this.ExplicitCaptureChkBox.Name = "ExplicitCaptureChkBox";
            
this.ExplicitCaptureChkBox.Size = new System.Drawing.Size(12016);
            
this.ExplicitCaptureChkBox.TabIndex = 5;
            
this.ExplicitCaptureChkBox.Text = "ExplicitCapture";
            
// 
            
// IgnoreCaseChkBox
            
// 
            this.IgnoreCaseChkBox.Location = new System.Drawing.Point(11232);
            
this.IgnoreCaseChkBox.Name = "IgnoreCaseChkBox";
            
this.IgnoreCaseChkBox.Size = new System.Drawing.Size(9616);
            
this.IgnoreCaseChkBox.TabIndex = 4;
            
this.IgnoreCaseChkBox.Text = "IgnoreCase";
            
// 
            
// IgnorePatternWhiteSpaceChkBox
            
// 
            this.IgnorePatternWhiteSpaceChkBox.Location = new System.Drawing.Point(1648);
            
this.IgnorePatternWhiteSpaceChkBox.Name = "IgnorePatternWhiteSpaceChkBox";
            
this.IgnorePatternWhiteSpaceChkBox.Size = new System.Drawing.Size(17616);
            
this.IgnorePatternWhiteSpaceChkBox.TabIndex = 3;
            
this.IgnorePatternWhiteSpaceChkBox.Text = "IgnorePatternWhiteSpace";
            
// 
            
// MultiLineChkBox
            
// 
            this.MultiLineChkBox.Location = new System.Drawing.Point(21616);
            
this.MultiLineChkBox.Name = "MultiLineChkBox";
            
this.MultiLineChkBox.Size = new System.Drawing.Size(8016);
            
this.MultiLineChkBox.TabIndex = 2;
            
this.MultiLineChkBox.Text = "MultiLine";
            
// 
            
// RightToLeftChkBox
            
// 
            this.RightToLeftChkBox.Location = new System.Drawing.Point(11216);
            
this.RightToLeftChkBox.Name = "RightToLeftChkBox";
            
this.RightToLeftChkBox.Size = new System.Drawing.Size(9616);
            
this.RightToLeftChkBox.TabIndex = 1;
            
this.RightToLeftChkBox.Text = "RightToLeft";
            
// 
            
// SingleLineChkBox
            
// 
            this.SingleLineChkBox.Location = new System.Drawing.Point(1616);
            
this.SingleLineChkBox.Name = "SingleLineChkBox";
            
this.SingleLineChkBox.Size = new System.Drawing.Size(8816);
            
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(8160);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(11216);
            
this.label1.TabIndex = 11;
            
this.label1.Text = "Text to Match On";
            
// 
            
// label2
            
// 
            this.label2.Location = new System.Drawing.Point(8264);
            
this.label2.Name = "label2";
            
this.label2.Size = new System.Drawing.Size(11216);
            
this.label2.TabIndex = 12;
            
this.label2.Text = "Replacement Text";
            
// 
            
// label3
            
// 
            this.label3.Location = new System.Drawing.Point(8344);
            
this.label3.Name = "label3";
            
this.label3.Size = new System.Drawing.Size(11216);
            
this.label3.TabIndex = 13;
            
this.label3.Text = "Results of Match";
            
// 
            
// label4
            
// 
            this.label4.Location = new System.Drawing.Point(80);
            
this.label4.Name = "label4";
            
this.label4.Size = new System.Drawing.Size(20016);
            
this.label4.TabIndex = 14;
            
this.label4.Text = "Regular Expression";
            
// 
            
// RegexTesterForm
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.ClientSize = new System.Drawing.Size(448469);
            
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();

            }
        }


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值