中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
完成C#正则表达式测试器
作者:未知 时间:2005-09-13 23:29 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:完成C#正则表达式测试器
输入正则表达式,测试文本,输出经表达式过滤的文本,第一个实用C#窗口程序,可用csc批处理程序直接编译

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
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.TextBox RegexTextBox;
  private System.Windows.Forms.GroupBox OptionsGroup;
  private System.Windows.Forms.CheckBox ECMAScriptChkBox;
  private System.Windows.Forms.CheckBox ExplicitCaptureChkBox;
  private System.Windows.Forms.CheckBox IgnoreCaseChkBox;
  private System.Windows.Forms.CheckBox MultiLineChkBox;
  private System.Windows.Forms.CheckBox RightToLeftChkBox;
  private System.Windows.Forms.CheckBox SingleLineChkBox;
  private System.Windows.Forms.CheckBox IgnorePatternWhiteSpaceChkBox;
  private System.Windows.Forms.Button OpenRegexButton;
  private System.Windows.Forms.Button SaveRegexButton;
  private System.Windows.Forms.TextBox InputTextBox;
  private System.Windows.Forms.TextBox ReplacementTextBox;
  private System.Windows.Forms.TextBox ResultTextBox;
  private System.Windows.Forms.Button TestRegaxButton;
  private System.Windows.Forms.Button ReplaceButton;
  private System.Windows.Forms.Button SplitButton;
  private System.Windows.Forms.Button MatchesButton;
  ///
  /// 必需的设计器变量。
  ///

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  ///


  /// 清理所有正在使用的资源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.RegexTextBox = new System.Windows.Forms.TextBox();
   this.OptionsGroup = new System.Windows.Forms.GroupBox();
   this.IgnorePatternWhiteSpaceChkBox = new System.Windows.Forms.CheckBox();
   this.SingleLineChkBox = new System.Windows.Forms.CheckBox();
   this.RightToLeftChkBox = new System.Windows.Forms.CheckBox();
   this.MultiLineChkBox = new System.Windows.Forms.CheckBox();
   this.IgnoreCaseChkBox = new System.Windows.Forms.CheckBox();
   this.ExplicitCaptureChkBox = new System.Windows.Forms.CheckBox();
   this.ECMAScriptChkBox = new System.Windows.Forms.CheckBox();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   this.OpenRegexButton = new System.Windows.Forms.Button();
   this.SaveRegexButton = new System.Windows.Forms.Button();
   this.label2 = new System.Windows.Forms.Label();
   this.InputTextBox = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.ReplacementTextBox = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.ResultTextBox = new System.Windows.Forms.TextBox();
   this.TestRegaxButton = 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.OptionsGroup.SuspendLayout();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(128, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "Regular Expression";
   //
   // RegexTextBox
   //
   this.RegexTextBox.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.RegexTextBox.Location = new System.Drawing.Point(16, 32);
   this.RegexTextBox.Multiline = true;
   this.RegexTextBox.Name = "RegexTextBox";
   this.RegexTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.RegexTextBox.Size = new System.Drawing.Size(624, 40);
   this.RegexTextBox.TabIndex = 1;
   this.RegexTextBox.Text = "";
   //
   // OptionsGroup
   //
   this.OptionsGroup.Controls.Add(this.IgnorePatternWhiteSpaceChkBox);
   this.OptionsGroup.Controls.Add(this.SingleLineChkBox);
   this.OptionsGroup.Controls.Add(this.RightToLeftChkBox);
   this.OptionsGroup.Controls.Add(this.MultiLineChkBox);
   this.OptionsGroup.Controls.Add(this.IgnoreCaseChkBox);
   this.OptionsGroup.Controls.Add(this.ExplicitCaptureChkBox);
   this.OptionsGroup.Controls.Add(this.ECMAScriptChkBox);
   this.OptionsGroup.Location = new System.Drawing.Point(16, 80);
   this.OptionsGroup.Name = "OptionsGroup";
   this.OptionsGroup.Size = new System.Drawing.Size(456, 104);
   this.OptionsGroup.TabIndex = 2;
   this.OptionsGroup.TabStop = false;
   this.OptionsGroup.Text = "Regax Options";
   //
   // IgnorePatternWhiteSpaceChkBox
   //
   this.IgnorePatternWhiteSpaceChkBox.Location = new System.Drawing.Point(24, 80);
   this.IgnorePatternWhiteSpaceChkBox.Name = "IgnorePatternWhiteSpaceChkBox";
   this.IgnorePatternWhiteSpaceChkBox.Size = new System.Drawing.Size(184, 16);
   this.IgnorePatternWhiteSpaceChkBox.TabIndex = 6;
   this.IgnorePatternWhiteSpaceChkBox.Text = "IgnorePatternWhiteSpace";
   //
   // SingleLineChkBox
   //
   this.SingleLineChkBox.Location = new System.Drawing.Point(320, 56);
   this.SingleLineChkBox.Name = "SingleLineChkBox";
   this.SingleLineChkBox.Size = new System.Drawing.Size(96, 16);
   this.SingleLineChkBox.TabIndex = 5;
   this.SingleLineChkBox.Text = "SingleLine";
   //
   // RightToLeftChkBox
   //
   this.RightToLeftChkBox.Location = new System.Drawing.Point(160, 56);
   this.RightToLeftChkBox.Name = "RightToLeftChkBox";
   this.RightToLeftChkBox.Size = new System.Drawing.Size(120, 16);
   this.RightToLeftChkBox.TabIndex = 4;
   this.RightToLeftChkBox.Text = "RightToLeft";
   //
   // MultiLineChkBox
   //
   this.MultiLineChkBox.Location = new System.Drawing.Point(24, 56);
   this.MultiLineChkBox.Name = "MultiLineChkBox";
   this.MultiLineChkBox.Size = new System.Drawing.Size(80, 16);
   this.MultiLineChkBox.TabIndex = 3;
   this.MultiLineChkBox.Text = "MultiLine";
   //
   // IgnoreCaseChkBox
   //
   this.IgnoreCaseChkBox.Location = new System.Drawing.Point(320, 32);
   this.IgnoreCaseChkBox.Name = "IgnoreCaseChkBox";
   this.IgnoreCaseChkBox.Size = new System.Drawing.Size(104, 16);
   this.IgnoreCaseChkBox.TabIndex = 2;
   this.IgnoreCaseChkBox.Text = "IgnoreCase";
   //
   // ExplicitCaptureChkBox
   //
   this.ExplicitCaptureChkBox.Location = new System.Drawing.Point(160, 32);
   this.ExplicitCaptureChkBox.Name = "ExplicitCaptureChkBox";
   this.ExplicitCaptureChkBox.Size = new System.Drawing.Size(128, 16);
   this.ExplicitCaptureChkBox.TabIndex = 1;
   this.ExplicitCaptureChkBox.Text = "Explicit Capture";
   //
   // ECMAScriptChkBox
   //
   this.ECMAScriptChkBox.Location = new System.Drawing.Point(24, 32);
   this.ECMAScriptChkBox.Name = "ECMAScriptChkBox";
   this.ECMAScriptChkBox.Size = new System.Drawing.Size(88, 16);
   this.ECMAScriptChkBox.TabIndex = 0;
   this.ECMAScriptChkBox.Text = "ECMAScript";
   //
   // openFileDialog1
   //
   this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
   //
   // saveFileDialog1
   //
   this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
   //
   // OpenRegexButton
   //
   this.OpenRegexButton.Location = new System.Drawing.Point(528, 96);
   this.OpenRegexButton.Name = "OpenRegexButton";
   this.OpenRegexButton.Size = new System.Drawing.Size(80, 24);
   this.OpenRegexButton.TabIndex = 3;
   this.OpenRegexButton.Text = "Open Regax";
   this.OpenRegexButton.Click += new System.EventHandler(this.OpenRegexButton_Click);
   //
   // SaveRegexButton
   //
   this.SaveRegexButton.Location = new System.Drawing.Point(528, 144);
   this.SaveRegexButton.Name = "SaveRegexButton";
   this.SaveRegexButton.Size = new System.Drawing.Size(80, 24);
   this.SaveRegexButton.TabIndex = 4;
   this.SaveRegexButton.Text = "Save Regax";
   this.SaveRegexButton.Click += new System.EventHandler(this.SaveRegexButton_Click);
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(16, 192);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(136, 16);
   this.label2.TabIndex = 5;
   this.label2.Text = "Text to Match On ";
   //
   // InputTextBox
   //
   this.InputTextBox.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.InputTextBox.Location = new System.Drawing.Point(16, 216);
   this.InputTextBox.Multiline = true;
   this.InputTextBox.Name = "InputTextBox";
   this.InputTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.InputTextBox.Size = new System.Drawing.Size(624, 88);
   this.InputTextBox.TabIndex = 6;
   this.InputTextBox.Text = "";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(16, 312);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(128, 16);
   this.label3.TabIndex = 7;
   this.label3.Text = "Replacement Text";
   //
   // ReplacementTextBox
   //
   this.ReplacementTextBox.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.ReplacementTextBox.Location = new System.Drawing.Point(16, 336);
   this.ReplacementTextBox.Multiline = true;
   this.ReplacementTextBox.Name = "ReplacementTextBox";
   this.ReplacementTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.ReplacementTextBox.Size = new System.Drawing.Size(624, 40);
   this.ReplacementTextBox.TabIndex = 8;
   this.ReplacementTextBox.Text = "";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(16, 384);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(120, 16);
   this.label4.TabIndex = 9;
   this.label4.Text = "Resulsts of Match";
   //
   // ResultTextBox
   //
   this.ResultTextBox.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.ResultTextBox.Location = new System.Drawing.Point(16, 408);
   this.ResultTextBox.Multiline = true;
   this.ResultTextBox.Name = "ResultTextBox";
   this.ResultTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.ResultTextBox.Size = new System.Drawing.Size(624, 72);
   this.ResultTextBox.TabIndex = 10;
   this.ResultTextBox.Text = "";
   //
   // TestRegaxButton
   //
   this.TestRegaxButton.Location = new System.Drawing.Point(40, 496);
   this.TestRegaxButton.Name = "TestRegaxButton";
   this.TestRegaxButton.Size = new System.Drawing.Size(112, 24);
   this.TestRegaxButton.TabIndex = 11;
   this.TestRegaxButton.Text = "IsMatch()";
   this.TestRegaxButton.Click += new System.EventHandler(this.TestRegaxButton_Click);
   //
   // ReplaceButton
   //
   this.ReplaceButton.Location = new System.Drawing.Point(184, 496);
   this.ReplaceButton.Name = "ReplaceButton";
   this.ReplaceButton.Size = new System.Drawing.Size(112, 24);
   this.ReplaceButton.TabIndex = 12;
   this.ReplaceButton.Text = "Replace()";
   this.ReplaceButton.Click += new System.EventHandler(this.ReplaceButton_Click);
   //
   // SplitButton
   //
   this.SplitButton.Location = new System.Drawing.Point(328, 496);
   this.SplitButton.Name = "SplitButton";
   this.SplitButton.Size = new System.Drawing.Size(120, 24);
   this.SplitButton.TabIndex = 13;
   this.SplitButton.Text = "Split()";
   this.SplitButton.Click += new System.EventHandler(this.SplitButton_Click);
   //
   // MatchesButton
   //
   this.MatchesButton.Location = new System.Drawing.Point(488, 496);
   this.MatchesButton.Name = "MatchesButton";
   this.MatchesButton.Size = new System.Drawing.Size(120, 24);
   this.MatchesButton.TabIndex = 14;
   this.MatchesButton.Text = "Matches()";
   this.MatchesButton.Click += new System.EventHandler(this.MatchesButton_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(656, 535);
   this.Controls.Add(this.MatchesButton);
   this.Controls.Add(this.SplitButton);
   this.Controls.Add(this.ReplaceButton);
   this.Controls.Add(this.TestRegaxButton);
   this.Controls.Add(this.ResultTextBox);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.ReplacementTextBox);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.InputTextBox);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.SaveRegexButton);
   this.Controls.Add(this.OpenRegexButton);
   this.Controls.Add(this.OptionsGroup);
   this.Controls.Add(this.RegexTextBox);
   this.Controls.Add(this.label1);
   this.MaximizeBox = false;
   this.Name = "Form1";
   this.Text = "Regular Expression Tester";
   this.OptionsGroup.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  ///


  /// 应用程序的主入口点。
  ///

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void SaveRegexButton_Click(object sender, System.EventArgs e) {
   saveFileDialog1.ShowDialog();
  }

  private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
   StreamWriter streamWriterRegex=File.CreateText(saveFileDialog1.FileName);
   streamWriterRegex.Write(this.RegexTextBox.Text);
   streamWriterRegex.Close();
  }

  private void OpenRegexButton_Click(object sender, System.EventArgs e) {
   openFileDialog1.ShowDialog();
  }

  private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
   StreamReader streamReaderRegex=File.OpenText(openFileDialog1.FileName);
   this.RegexTextBox.Text=streamReaderRegex.ReadToEnd();
   streamReaderRegex.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 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.ResultTextBox.ForeColor=Color.Black;
    this.RegexTextBox.Text=resultsString.ToString();
   }
   catch(ArgumentException ex){
    this.ResultTextBox.ForeColor=Color.Red;
    this.RegexTextBox.Text="There was an error in you regular expression:\r\n"+ex.Message;
   }

  }

  private void TestRegaxButton_Click(object sender, System.EventArgs e) {
   try{
    RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
    Regex testRegex=new Regex(this.RegexTextBox.Text,selectedRegexOptions);
    if(testRegex.IsMatch(this.InputTextBox.Text)){
     this.ResultTextBox.ForeColor=Color.Black;
     this.ResultTextBox.Text="MATCH FOUND";
    }
    else{
     this.ResultTextBox.ForeColor=Color.Red;
     this.ResultTextBox.Text="NOT METCH FOUND";
    }
   }
   catch(ArgumentException ex){
    this.ResultTextBox.Text="There waw an error in you regular expression:\r\n"+ex.Message;
   }

  }

  private void ReplaceButton_Click(object sender, System.EventArgs e) {
   try{
    RegexOptions selectedRegexOptions=this.GetSelectedRegexOptions();
    Regex replaceRegex=new Regex(this.ResultTextBox.Text,selectedRegexOptions);
    this.ResultTextBox.ForeColor=Color.Black;
    this.ResultTextBox.Text=replaceRegex.Replace(this.InputTextBox.Text,this.ReplacementTextBox.Text);
   }
   catch(ArgumentException ex){
    this.ResultTextBox.ForeColor=Color.Red;
    this.ResultTextBox.Text="There was an error in you regular expression:\r\n"+ex.Message;
   }

  }

  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--------------------------\r\n";
    StringBuilder resultsString=new StringBuilder(64);
    foreach(Match matchMade in matchesFound)
     resultsString.Append(matchMade.Value+(Environment.NewLine+nextMatch));
    this.ResultTextBox.ForeColor=Color.Black;
    this.ResultTextBox.Text=resultsString.ToString();
   }
   catch(ArgumentException ex){
    this.ResultTextBox.ForeColor=Color.Red;
    this.ResultTextBox.Text="There was an error in your rgular expression:\r\n"+ex.Message;
   }

  }

 }
}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有