Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Regular Expression Library Builder

, 14 Jun 2005
A tool to quickly allow the creation and modification of .NET Regular Expression Libraries
regexlibrarybuilder_application.zip
regexlibrarybuilder_example.zip
regexlibrarybuilder_source.zip
RegexLibraryBuilder
Graphics
App.ico
Delete.bmp
NewDocument.bmp
OpenFolder.bmp
Save.bmp
using System;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using Dela.RegexLibraryBuilder;
using Dela.RegexLibraryBuilder.Collections;

namespace Dela.RegexLibraryBuilder.Gui
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class RegexLibraryBuilderForm : System.Windows.Forms.Form
	{
      #region GUI Elements
      
      private System.Windows.Forms.MainMenu mainMenu;
      private System.Windows.Forms.MenuItem menuItemFileMenu;
      private System.Windows.Forms.MenuItem menuItemLoad;
      private System.Windows.Forms.MenuItem menuItemSave;
      private System.Windows.Forms.MenuItem menuItemFileMenuSeperator;
      private System.Windows.Forms.MenuItem menuItemExit;
      private System.Windows.Forms.MenuItem menuItemRegexMenu;
      private System.Windows.Forms.MenuItem menuItemNewRegex;
      private System.Windows.Forms.MenuItem menuItemDeleteRegex;
      private System.Windows.Forms.MenuItem menuItemHelpMenu;
      private System.Windows.Forms.MenuItem menuItemAbout;
      private System.Windows.Forms.ToolBar toolBar;
      private System.Windows.Forms.GroupBox groupBoxAssemblyDetails;
      private System.Windows.Forms.ImageList imageListToolbar;
      private System.Windows.Forms.ToolBarButton toolBarButtonNew;
      private System.Windows.Forms.ToolBarButton toolBarButtonOpen;
      private System.Windows.Forms.ToolBarButton toolBarButtonSave;
      private System.Windows.Forms.ToolBarButton toolBarButton1;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.TextBox txtAssemblyFileName;
      private System.Windows.Forms.TextBox txtAssemblyVersion;
      private System.Windows.Forms.ToolBarButton toolBarButtonDelete;
      private System.Windows.Forms.ListView listViewRegexs;
      private System.Windows.Forms.ColumnHeader colRegex;
      private System.Windows.Forms.ColumnHeader colRegexOptions;
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.TextBox txtRegex;
      private System.Windows.Forms.Button btnDelete;
      private System.Windows.Forms.Button btnAdd;
      private System.Windows.Forms.CheckBox chkECMAScript;
      private System.Windows.Forms.CheckBox chkExplicit;
      private System.Windows.Forms.CheckBox chkRightToLeft;
      private System.Windows.Forms.CheckBox chkMultiline;
      private System.Windows.Forms.CheckBox chkIgnoreCase;
      private System.Windows.Forms.CheckBox chkIgnoreWhiteSace;
      private System.Windows.Forms.CheckBox chkCompiled;
      private System.Windows.Forms.CheckBox chkSingleLine;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.TextBox txtRegexName;
      private System.ComponentModel.IContainer components;
      private System.Windows.Forms.ColumnHeader colRegexName;
      private System.Windows.Forms.Button btnBrowse;
      private System.Windows.Forms.TextBox txtNamespace;
      private System.Windows.Forms.Label label5;
      private System.Windows.Forms.CheckBox chkPublic;

      #endregion

      #region Fields

      private RegexLibraryBuilder _libraryBuilder = new RegexLibraryBuilder();

      #endregion

      #region Constructors

		public RegexLibraryBuilderForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

         this.Text += " - v" + Assembly.GetExecutingAssembly().GetName().Version + " beta";

         _libraryBuilder.LibraryLoaded += new Dela.RegexLibraryBuilder.RegexLibraryBuilder.LibraryLoadedDelegate(libraryBuilder_LibraryLoaded);
		}

      #endregion

      #region Protected Methods

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

      #endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.components = new System.ComponentModel.Container();
         System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(RegexLibraryBuilderForm));
         this.mainMenu = new System.Windows.Forms.MainMenu();
         this.menuItemFileMenu = new System.Windows.Forms.MenuItem();
         this.menuItemLoad = new System.Windows.Forms.MenuItem();
         this.menuItemSave = new System.Windows.Forms.MenuItem();
         this.menuItemFileMenuSeperator = new System.Windows.Forms.MenuItem();
         this.menuItemExit = new System.Windows.Forms.MenuItem();
         this.menuItemRegexMenu = new System.Windows.Forms.MenuItem();
         this.menuItemNewRegex = new System.Windows.Forms.MenuItem();
         this.menuItemDeleteRegex = new System.Windows.Forms.MenuItem();
         this.menuItemHelpMenu = new System.Windows.Forms.MenuItem();
         this.menuItemAbout = new System.Windows.Forms.MenuItem();
         this.toolBar = new System.Windows.Forms.ToolBar();
         this.toolBarButtonOpen = new System.Windows.Forms.ToolBarButton();
         this.toolBarButtonSave = new System.Windows.Forms.ToolBarButton();
         this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
         this.toolBarButtonNew = new System.Windows.Forms.ToolBarButton();
         this.toolBarButtonDelete = new System.Windows.Forms.ToolBarButton();
         this.imageListToolbar = new System.Windows.Forms.ImageList(this.components);
         this.groupBoxAssemblyDetails = new System.Windows.Forms.GroupBox();
         this.btnBrowse = new System.Windows.Forms.Button();
         this.txtAssemblyVersion = new System.Windows.Forms.TextBox();
         this.txtAssemblyFileName = new System.Windows.Forms.TextBox();
         this.label2 = new System.Windows.Forms.Label();
         this.label1 = new System.Windows.Forms.Label();
         this.listViewRegexs = new System.Windows.Forms.ListView();
         this.colRegexName = new System.Windows.Forms.ColumnHeader();
         this.colRegex = new System.Windows.Forms.ColumnHeader();
         this.colRegexOptions = new System.Windows.Forms.ColumnHeader();
         this.groupBox1 = new System.Windows.Forms.GroupBox();
         this.chkPublic = new System.Windows.Forms.CheckBox();
         this.txtNamespace = new System.Windows.Forms.TextBox();
         this.label5 = new System.Windows.Forms.Label();
         this.txtRegexName = new System.Windows.Forms.TextBox();
         this.label3 = new System.Windows.Forms.Label();
         this.btnDelete = new System.Windows.Forms.Button();
         this.btnAdd = new System.Windows.Forms.Button();
         this.chkECMAScript = new System.Windows.Forms.CheckBox();
         this.chkExplicit = new System.Windows.Forms.CheckBox();
         this.chkRightToLeft = new System.Windows.Forms.CheckBox();
         this.chkMultiline = new System.Windows.Forms.CheckBox();
         this.chkIgnoreCase = new System.Windows.Forms.CheckBox();
         this.chkIgnoreWhiteSace = new System.Windows.Forms.CheckBox();
         this.chkCompiled = new System.Windows.Forms.CheckBox();
         this.chkSingleLine = new System.Windows.Forms.CheckBox();
         this.txtRegex = new System.Windows.Forms.TextBox();
         this.label4 = new System.Windows.Forms.Label();
         this.groupBoxAssemblyDetails.SuspendLayout();
         this.groupBox1.SuspendLayout();
         this.SuspendLayout();
         // 
         // mainMenu
         // 
         this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                 this.menuItemFileMenu,
                                                                                 this.menuItemRegexMenu,
                                                                                 this.menuItemHelpMenu});
         // 
         // menuItemFileMenu
         // 
         this.menuItemFileMenu.Index = 0;
         this.menuItemFileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                         this.menuItemLoad,
                                                                                         this.menuItemSave,
                                                                                         this.menuItemFileMenuSeperator,
                                                                                         this.menuItemExit});
         this.menuItemFileMenu.Text = "&File";
         // 
         // menuItemLoad
         // 
         this.menuItemLoad.Index = 0;
         this.menuItemLoad.Text = "&Load Regex Library";
         this.menuItemLoad.Click += new System.EventHandler(this.menuItemLoad_Click);
         // 
         // menuItemSave
         // 
         this.menuItemSave.Index = 1;
         this.menuItemSave.Text = "&Save Regex Library";
         this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
         // 
         // menuItemFileMenuSeperator
         // 
         this.menuItemFileMenuSeperator.Index = 2;
         this.menuItemFileMenuSeperator.Text = "-";
         // 
         // menuItemExit
         // 
         this.menuItemExit.Index = 3;
         this.menuItemExit.Text = "&Exit";
         this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
         // 
         // menuItemRegexMenu
         // 
         this.menuItemRegexMenu.Index = 1;
         this.menuItemRegexMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                          this.menuItemNewRegex,
                                                                                          this.menuItemDeleteRegex});
         this.menuItemRegexMenu.Text = "&Regex";
         // 
         // menuItemNewRegex
         // 
         this.menuItemNewRegex.Index = 0;
         this.menuItemNewRegex.Text = "&New Regex";
         this.menuItemNewRegex.Click += new System.EventHandler(this.menuItemNewRegex_Click);
         // 
         // menuItemDeleteRegex
         // 
         this.menuItemDeleteRegex.Enabled = false;
         this.menuItemDeleteRegex.Index = 1;
         this.menuItemDeleteRegex.Text = "&Delete Regex";
         this.menuItemDeleteRegex.Click += new System.EventHandler(this.menuItemDeleteRegex_Click);
         // 
         // menuItemHelpMenu
         // 
         this.menuItemHelpMenu.Index = 2;
         this.menuItemHelpMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                         this.menuItemAbout});
         this.menuItemHelpMenu.Text = "&Help";
         // 
         // menuItemAbout
         // 
         this.menuItemAbout.Index = 0;
         this.menuItemAbout.Text = "&About";
         this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
         // 
         // toolBar
         // 
         this.toolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
         this.toolBar.AutoSize = false;
         this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                                                                   this.toolBarButtonOpen,
                                                                                   this.toolBarButtonSave,
                                                                                   this.toolBarButton1,
                                                                                   this.toolBarButtonNew,
                                                                                   this.toolBarButtonDelete});
         this.toolBar.ButtonSize = new System.Drawing.Size(20, 36);
         this.toolBar.DropDownArrows = true;
         this.toolBar.ImageList = this.imageListToolbar;
         this.toolBar.Location = new System.Drawing.Point(0, 0);
         this.toolBar.Name = "toolBar";
         this.toolBar.ShowToolTips = true;
         this.toolBar.Size = new System.Drawing.Size(594, 25);
         this.toolBar.TabIndex = 0;
         this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
         // 
         // toolBarButtonOpen
         // 
         this.toolBarButtonOpen.ImageIndex = 0;
         this.toolBarButtonOpen.ToolTipText = "Open RegexLibrary";
         // 
         // toolBarButtonSave
         // 
         this.toolBarButtonSave.ImageIndex = 2;
         this.toolBarButtonSave.ToolTipText = "Save Regex Library";
         // 
         // toolBarButton1
         // 
         this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
         // 
         // toolBarButtonNew
         // 
         this.toolBarButtonNew.ImageIndex = 1;
         this.toolBarButtonNew.ToolTipText = "New Regex";
         // 
         // toolBarButtonDelete
         // 
         this.toolBarButtonDelete.Enabled = false;
         this.toolBarButtonDelete.ImageIndex = 3;
         this.toolBarButtonDelete.ToolTipText = "Delete Selected Regex";
         // 
         // imageListToolbar
         // 
         this.imageListToolbar.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit;
         this.imageListToolbar.ImageSize = new System.Drawing.Size(16, 16);
         this.imageListToolbar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListToolbar.ImageStream")));
         this.imageListToolbar.TransparentColor = System.Drawing.Color.Magenta;
         // 
         // groupBoxAssemblyDetails
         // 
         this.groupBoxAssemblyDetails.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.groupBoxAssemblyDetails.Controls.Add(this.btnBrowse);
         this.groupBoxAssemblyDetails.Controls.Add(this.txtAssemblyVersion);
         this.groupBoxAssemblyDetails.Controls.Add(this.txtAssemblyFileName);
         this.groupBoxAssemblyDetails.Controls.Add(this.label2);
         this.groupBoxAssemblyDetails.Controls.Add(this.label1);
         this.groupBoxAssemblyDetails.Location = new System.Drawing.Point(8, 32);
         this.groupBoxAssemblyDetails.Name = "groupBoxAssemblyDetails";
         this.groupBoxAssemblyDetails.Size = new System.Drawing.Size(576, 80);
         this.groupBoxAssemblyDetails.TabIndex = 1;
         this.groupBoxAssemblyDetails.TabStop = false;
         this.groupBoxAssemblyDetails.Text = "Regex Library Options";
         // 
         // btnBrowse
         // 
         this.btnBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
         this.btnBrowse.Location = new System.Drawing.Point(536, 24);
         this.btnBrowse.Name = "btnBrowse";
         this.btnBrowse.Size = new System.Drawing.Size(32, 23);
         this.btnBrowse.TabIndex = 4;
         this.btnBrowse.Text = "...";
         this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
         // 
         // txtAssemblyVersion
         // 
         this.txtAssemblyVersion.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtAssemblyVersion.Location = new System.Drawing.Point(128, 48);
         this.txtAssemblyVersion.Name = "txtAssemblyVersion";
         this.txtAssemblyVersion.Size = new System.Drawing.Size(214, 20);
         this.txtAssemblyVersion.TabIndex = 3;
         this.txtAssemblyVersion.Text = "";
         // 
         // txtAssemblyFileName
         // 
         this.txtAssemblyFileName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtAssemblyFileName.Location = new System.Drawing.Point(128, 24);
         this.txtAssemblyFileName.Name = "txtAssemblyFileName";
         this.txtAssemblyFileName.Size = new System.Drawing.Size(382, 20);
         this.txtAssemblyFileName.TabIndex = 2;
         this.txtAssemblyFileName.Text = "";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(16, 48);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(112, 23);
         this.label2.TabIndex = 1;
         this.label2.Text = "Assembly Version:";
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(16, 24);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(112, 23);
         this.label1.TabIndex = 0;
         this.label1.Text = "Assembly File Name:";
         // 
         // listViewRegexs
         // 
         this.listViewRegexs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.listViewRegexs.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                         this.colRegexName,
                                                                                         this.colRegex,
                                                                                         this.colRegexOptions});
         this.listViewRegexs.FullRowSelect = true;
         this.listViewRegexs.Location = new System.Drawing.Point(8, 330);
         this.listViewRegexs.MultiSelect = false;
         this.listViewRegexs.Name = "listViewRegexs";
         this.listViewRegexs.Size = new System.Drawing.Size(576, 176);
         this.listViewRegexs.TabIndex = 2;
         this.listViewRegexs.View = System.Windows.Forms.View.Details;
         this.listViewRegexs.SelectedIndexChanged += new System.EventHandler(this.listViewRegexs_SelectedIndexChanged);
         // 
         // colRegexName
         // 
         this.colRegexName.Text = "Name";
         this.colRegexName.Width = 100;
         // 
         // colRegex
         // 
         this.colRegex.Text = "Regex";
         this.colRegex.Width = 322;
         // 
         // colRegexOptions
         // 
         this.colRegexOptions.Text = "Regex Options";
         this.colRegexOptions.Width = 150;
         // 
         // groupBox1
         // 
         this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.groupBox1.Controls.Add(this.chkPublic);
         this.groupBox1.Controls.Add(this.txtNamespace);
         this.groupBox1.Controls.Add(this.label5);
         this.groupBox1.Controls.Add(this.txtRegexName);
         this.groupBox1.Controls.Add(this.label3);
         this.groupBox1.Controls.Add(this.btnDelete);
         this.groupBox1.Controls.Add(this.btnAdd);
         this.groupBox1.Controls.Add(this.chkECMAScript);
         this.groupBox1.Controls.Add(this.chkExplicit);
         this.groupBox1.Controls.Add(this.chkRightToLeft);
         this.groupBox1.Controls.Add(this.chkMultiline);
         this.groupBox1.Controls.Add(this.chkIgnoreCase);
         this.groupBox1.Controls.Add(this.chkIgnoreWhiteSace);
         this.groupBox1.Controls.Add(this.chkCompiled);
         this.groupBox1.Controls.Add(this.chkSingleLine);
         this.groupBox1.Controls.Add(this.txtRegex);
         this.groupBox1.Controls.Add(this.label4);
         this.groupBox1.Location = new System.Drawing.Point(8, 112);
         this.groupBox1.Name = "groupBox1";
         this.groupBox1.Size = new System.Drawing.Size(576, 210);
         this.groupBox1.TabIndex = 3;
         this.groupBox1.TabStop = false;
         this.groupBox1.Text = "Regex";
         // 
         // chkPublic
         // 
         this.chkPublic.Checked = true;
         this.chkPublic.CheckState = System.Windows.Forms.CheckState.Checked;
         this.chkPublic.Location = new System.Drawing.Point(16, 176);
         this.chkPublic.Name = "chkPublic";
         this.chkPublic.TabIndex = 15;
         this.chkPublic.Text = "Public";
         // 
         // txtNamespace
         // 
         this.txtNamespace.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtNamespace.Location = new System.Drawing.Point(80, 152);
         this.txtNamespace.Name = "txtNamespace";
         this.txtNamespace.Size = new System.Drawing.Size(486, 20);
         this.txtNamespace.TabIndex = 14;
         this.txtNamespace.Text = "";
         // 
         // label5
         // 
         this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.label5.Location = new System.Drawing.Point(8, 152);
         this.label5.Name = "label5";
         this.label5.Size = new System.Drawing.Size(80, 23);
         this.label5.TabIndex = 17;
         this.label5.Text = "Namespace:";
         // 
         // txtRegexName
         // 
         this.txtRegexName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtRegexName.Location = new System.Drawing.Point(80, 120);
         this.txtRegexName.Name = "txtRegexName";
         this.txtRegexName.Size = new System.Drawing.Size(486, 20);
         this.txtRegexName.TabIndex = 13;
         this.txtRegexName.Text = "";
         // 
         // label3
         // 
         this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.label3.Location = new System.Drawing.Point(8, 120);
         this.label3.Name = "label3";
         this.label3.Size = new System.Drawing.Size(80, 23);
         this.label3.TabIndex = 15;
         this.label3.Text = "Regex Name:";
         // 
         // btnDelete
         // 
         this.btnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.btnDelete.Enabled = false;
         this.btnDelete.Location = new System.Drawing.Point(307, 178);
         this.btnDelete.Name = "btnDelete";
         this.btnDelete.TabIndex = 17;
         this.btnDelete.Text = "Delete";
         this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
         // 
         // btnAdd
         // 
         this.btnAdd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.btnAdd.Location = new System.Drawing.Point(202, 178);
         this.btnAdd.Name = "btnAdd";
         this.btnAdd.TabIndex = 16;
         this.btnAdd.Text = "Add";
         this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
         // 
         // chkECMAScript
         // 
         this.chkECMAScript.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkECMAScript.Location = new System.Drawing.Point(277, 88);
         this.chkECMAScript.Name = "chkECMAScript";
         this.chkECMAScript.TabIndex = 11;
         this.chkECMAScript.Text = "ECMAScript";
         // 
         // chkExplicit
         // 
         this.chkExplicit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkExplicit.Location = new System.Drawing.Point(405, 88);
         this.chkExplicit.Name = "chkExplicit";
         this.chkExplicit.TabIndex = 12;
         this.chkExplicit.Text = "Explicit Capture";
         // 
         // chkRightToLeft
         // 
         this.chkRightToLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkRightToLeft.Location = new System.Drawing.Point(277, 64);
         this.chkRightToLeft.Name = "chkRightToLeft";
         this.chkRightToLeft.TabIndex = 7;
         this.chkRightToLeft.Text = "Right to Left";
         // 
         // chkMultiline
         // 
         this.chkMultiline.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkMultiline.Location = new System.Drawing.Point(141, 64);
         this.chkMultiline.Name = "chkMultiline";
         this.chkMultiline.TabIndex = 6;
         this.chkMultiline.Text = "Multiline";
         // 
         // chkIgnoreCase
         // 
         this.chkIgnoreCase.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkIgnoreCase.Location = new System.Drawing.Point(141, 88);
         this.chkIgnoreCase.Name = "chkIgnoreCase";
         this.chkIgnoreCase.TabIndex = 10;
         this.chkIgnoreCase.Text = "Ignore Case";
         // 
         // chkIgnoreWhiteSace
         // 
         this.chkIgnoreWhiteSace.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkIgnoreWhiteSace.Location = new System.Drawing.Point(405, 64);
         this.chkIgnoreWhiteSace.Name = "chkIgnoreWhiteSace";
         this.chkIgnoreWhiteSace.Size = new System.Drawing.Size(160, 24);
         this.chkIgnoreWhiteSace.TabIndex = 8;
         this.chkIgnoreWhiteSace.Text = "Ignore Pattern Whitespace";
         // 
         // chkCompiled
         // 
         this.chkCompiled.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkCompiled.Location = new System.Drawing.Point(13, 88);
         this.chkCompiled.Name = "chkCompiled";
         this.chkCompiled.TabIndex = 9;
         this.chkCompiled.Text = "Compiled";
         // 
         // chkSingleLine
         // 
         this.chkSingleLine.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.chkSingleLine.Location = new System.Drawing.Point(13, 64);
         this.chkSingleLine.Name = "chkSingleLine";
         this.chkSingleLine.TabIndex = 5;
         this.chkSingleLine.Text = "Single Line";
         // 
         // txtRegex
         // 
         this.txtRegex.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtRegex.Location = new System.Drawing.Point(80, 16);
         this.txtRegex.Multiline = true;
         this.txtRegex.Name = "txtRegex";
         this.txtRegex.Size = new System.Drawing.Size(488, 48);
         this.txtRegex.TabIndex = 4;
         this.txtRegex.Text = "";
         // 
         // label4
         // 
         this.label4.Location = new System.Drawing.Point(16, 16);
         this.label4.Name = "label4";
         this.label4.Size = new System.Drawing.Size(56, 16);
         this.label4.TabIndex = 0;
         this.label4.Text = "Regex:";
         // 
         // RegexLibraryBuilderForm
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(594, 515);
         this.Controls.Add(this.groupBox1);
         this.Controls.Add(this.listViewRegexs);
         this.Controls.Add(this.groupBoxAssemblyDetails);
         this.Controls.Add(this.toolBar);
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
         this.Menu = this.mainMenu;
         this.Name = "RegexLibraryBuilderForm";
         this.Text = "RegexLibrary Builder";
         this.groupBoxAssemblyDetails.ResumeLayout(false);
         this.groupBox1.ResumeLayout(false);
         this.ResumeLayout(false);

      }
		#endregion

      #region Event Handlers

      #region GUI Event Handlers

      #region Button Handlers

      private void btnBrowse_Click(object sender, System.EventArgs e)
      {
         SaveFileDialog saveFileDialog = new SaveFileDialog();
         saveFileDialog.Filter = "(.dll)|*.dll";

         if(saveFileDialog.ShowDialog() == DialogResult.OK)
         {
            txtAssemblyFileName.Text = saveFileDialog.FileName;
         }
      }

      private void btnAdd_Click(object sender, System.EventArgs e)
      {
         if(txtRegex.Text.Length < 1)
         {
            MessageBox.Show("You must enter a regex!");
         }
         else if(txtRegexName.Text.Length < 1)
         {  
            MessageBox.Show("You must enter a regex name!");
         }
         else if(!Regex.IsMatch(txtRegexName.Text, "^[_a-zA-Z][_a-zA-Z0-9]*$"))
         {
            MessageBox.Show("Invalid regex name! Standard .NET class naming rules apply to regex names!");
         }
         else if(txtNamespace.Text.Length < 1)
         {
            MessageBox.Show("You must enter a namespace name!");
         }
         else if(!Regex.IsMatch(txtNamespace.Text, "^[_a-zA-Z][_.a-zA-Z0-9]*$"))
         {
            MessageBox.Show("Invalid namespace name! Standard .NET namespace naming rules apply!");
         }
         else if(RegexNameValid())
         {
            string errorMessage = string.Empty;
            if(IsValidRegex(txtRegex.Text, out errorMessage) == false)
            {
               MessageBox.Show(errorMessage, "Invalid Regex!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
               RegexOptions regexOptions = RegexOptions.None;

               regexOptions |= chkCompiled.Checked ? RegexOptions.Compiled : RegexOptions.None;
               regexOptions |= chkECMAScript.Checked ? RegexOptions.ECMAScript : RegexOptions.None;
               regexOptions |= chkExplicit.Checked ? RegexOptions.ExplicitCapture : RegexOptions.None;
               regexOptions |= chkIgnoreCase.Checked ? RegexOptions.IgnoreCase : RegexOptions.None;
               regexOptions |= chkIgnoreWhiteSace.Checked ? RegexOptions.IgnorePatternWhitespace : RegexOptions.None;
               regexOptions |= chkMultiline.Checked ? RegexOptions.Multiline : RegexOptions.None;
               regexOptions |= chkRightToLeft.Checked ? RegexOptions.RightToLeft : RegexOptions.None;
               regexOptions |= chkSingleLine.Checked ? RegexOptions.Singleline : RegexOptions.None;

               RegexCompilationInfo regexInfo = new RegexCompilationInfo(txtRegex.Text, regexOptions, txtRegexName.Text, txtNamespace.Text, chkPublic.Checked);

               ListViewItem item = new ListViewItem();
               item.Text = regexInfo.Name;
               item.SubItems.Add(regexInfo.Pattern);
               item.SubItems.Add(regexInfo.Options.ToString());
               item.Tag = regexInfo;
               
               listViewRegexs.Items.Add(item);
               
               ResetRegexGroupBox();
            }
         }
        
      }

      private void btnDelete_Click(object sender, System.EventArgs e)
      {
         if(listViewRegexs.SelectedItems.Count == 1)
         {
            DialogResult result = MessageBox.Show("Are you sure you want to delete regex '" + txtRegexName.Text + "'?", 
               "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(result == DialogResult.Yes)
            {
               listViewRegexs.Items.RemoveAt(listViewRegexs.SelectedIndices[0]);

               ResetRegexGroupBox();
            }
         }
      }

      #endregion

      #region Toolbar Handlers

      private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
      {
         if(e.Button == toolBarButtonOpen)
         {
            menuItemLoad_Click(sender, e);
         }
         else if(e.Button == toolBarButtonSave)
         {
            menuItemSave_Click(sender, e);
         }
         else if(e.Button == toolBarButtonNew)
         {
            menuItemNewRegex_Click(sender, e);
         }
         else if(e.Button == toolBarButtonDelete)
         {
            menuItemDeleteRegex_Click(sender, e);
         }
      }

      #endregion

      #region Menu Item Handlers

      private void menuItemLoad_Click(object sender, System.EventArgs e)
      {
         OpenFileDialog fileDialog = new OpenFileDialog();
         fileDialog.Filter = "Regex Library (*.dll)|*.dll";

         if(fileDialog.ShowDialog() == DialogResult.OK)
         {
            _libraryBuilder.LoadRegexLibrary(fileDialog.FileName);
            txtAssemblyFileName.Text = fileDialog.FileName;
         }
      }

      private void menuItemSave_Click(object sender, System.EventArgs e)
      {
         bool valid = false;

         if(Regex.IsMatch(txtAssemblyVersion.Text, @"^\d+(\.\d+){1,3}$"))
         {
            valid = true;
         }
         else
         {
            MessageBox.Show("Invalid Version Number!\r\nYou must specify at least a major and minor version number, e.g. 1.01,\r\nand at most 4 numbers, e.g. 1.01.8567.234");
         }

         if(File.Exists(txtAssemblyFileName.Text) && valid)
         {
            valid = DialogResult.Yes == MessageBox.Show("Are you sure you want to overwrite " + txtAssemblyFileName.Text + "?", "Confirm file replace", MessageBoxButtons.YesNo);
         }

         if(valid)
         {
            _libraryBuilder.Regexs.Clear();
            foreach(ListViewItem item in listViewRegexs.Items)
            {
               _libraryBuilder.Regexs.Add(item.Tag as RegexCompilationInfo);
            }

            _libraryBuilder.AssemblyName.Version = new Version(txtAssemblyVersion.Text);
            _libraryBuilder.AssemblyName.Name = Path.GetFileNameWithoutExtension(txtAssemblyFileName.Text);
            _libraryBuilder.AssemblyName.CodeBase = Path.GetDirectoryName(txtAssemblyFileName.Text);

            _libraryBuilder.SaveRegexLibrary();

            if(File.Exists(txtAssemblyFileName.Text))
            {
               MessageBox.Show("Regex Library Saved Successfully");
            }
         }
      }

      private void menuItemExit_Click(object sender, System.EventArgs e)
      {
         Application.Exit();
      }

      private void menuItemAbout_Click(object sender, System.EventArgs e)
      {
         AboutForm about = new AboutForm();
         about.StartPosition = FormStartPosition.CenterParent;
         about.ShowDialog();
      }

      private void menuItemNewRegex_Click(object sender, System.EventArgs e)
      {
         ResetRegexGroupBox();
      }

      private void menuItemDeleteRegex_Click(object sender, System.EventArgs e)
      {
         btnDelete_Click(sender, e);
      }

      #endregion

      #region Other GUI Events

      private void listViewRegexs_SelectedIndexChanged(object sender, System.EventArgs e)
      {
         if(listViewRegexs.SelectedItems.Count == 1)
         {
            RegexCompilationInfo regexInfo = listViewRegexs.SelectedItems[0].Tag as RegexCompilationInfo;
            if(regexInfo != null)
            {
               txtRegex.Text = regexInfo.Pattern;
               txtNamespace.Text = regexInfo.Namespace;
               txtRegexName.Text = regexInfo.Name;
               chkPublic.Checked = regexInfo.IsPublic;
                
               chkCompiled.Checked = (regexInfo.Options & RegexOptions.Compiled) == RegexOptions.Compiled;
               chkECMAScript.Checked = (regexInfo.Options & RegexOptions.ECMAScript) == RegexOptions.ECMAScript;
               chkExplicit.Checked = (regexInfo.Options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture;
               chkIgnoreCase.Checked = (regexInfo.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase;
               chkIgnoreWhiteSace.Checked = (regexInfo.Options & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace;
               chkMultiline.Checked = (regexInfo.Options & RegexOptions.Multiline) == RegexOptions.Multiline;
               chkRightToLeft.Checked = (regexInfo.Options & RegexOptions.RightToLeft) == RegexOptions.RightToLeft;
               chkSingleLine.Checked = (regexInfo.Options & RegexOptions.Singleline) == RegexOptions.Singleline;
            }
            btnDelete.Enabled = true;
            toolBarButtonDelete.Enabled = true;
            menuItemDeleteRegex.Enabled = true;
         }
         else
         {
            btnDelete.Enabled = false;
            toolBarButtonDelete.Enabled = false;
            menuItemDeleteRegex.Enabled = false;
         }
      }

      #endregion

      #endregion

      #region Non-GUI Event Handlers

      private void libraryBuilder_LibraryLoaded(RegexCompilationInfoCollection regexs, System.Reflection.AssemblyName assemblyName)
      {
         txtAssemblyVersion.Text = assemblyName.Version.ToString();
         listViewRegexs.Items.Clear();

         if(regexs != null && regexs.Count > 0)
         {
            foreach(RegexCompilationInfo regexInfo in regexs)
            {
               ListViewItem item = new ListViewItem();
               item.Text = regexInfo.Name;
               item.SubItems.Add(regexInfo.Pattern);
               item.SubItems.Add(regexInfo.Options.ToString());
               item.Tag = regexInfo;
               
               listViewRegexs.Items.Add(item);
            }
         }
         else
         {
            MessageBox.Show("Not a Regular Expression Library!");
         }
      }

      #endregion

      #endregion

      #region Internal Methods

      private void ResetRegexGroupBox()
      {
         // Reset Regex Group Box.
         txtRegex.Text = string.Empty;
         txtNamespace.Text = string.Empty;
         txtRegexName.Text = string.Empty;
         chkCompiled.Checked = false;
         chkECMAScript.Checked = false;
         chkExplicit.Checked  = false;
         chkIgnoreCase.Checked  = false;
         chkIgnoreWhiteSace.Checked  = false;
         chkMultiline.Checked  = false;
         chkRightToLeft.Checked  = false;
         chkSingleLine.Checked  = false;
      }

      private bool RegexNameValid()
      {
         bool valid = true;

         foreach(ListViewItem item in listViewRegexs.Items)
         {
            if(((RegexCompilationInfo)item.Tag).Namespace == txtNamespace.Text && item.Text == txtRegexName.Text)
            {
               DialogResult result =MessageBox.Show("A regex named '" + item.Text + "' already exists for the specified namespace. Are you sure you wish to overwrite it?", 
                  "Confirm Overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
               if(result == DialogResult.Yes)
               {
                  valid = true;
                  listViewRegexs.Items.Remove(item);
               }
               else
               {
                  valid = false;
               }
            }
         }

         return valid;
      }

      private bool IsValidRegex(string regex, out string errorMessage)
      {
         bool isValid = true;
         errorMessage = string.Empty;

         try
         {
            Regex.Match("", regex);
         }
         catch(ArgumentException exception)
         {
            errorMessage = exception.Message;
            isValid = false;
         }

         return isValid;
      }

      #endregion
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Brian Delahunty
Software Developer (Senior)
United States United States
Irish guy living in Seattle who is passionate about technology, running, trail running. Software Engineer @Microsoft by day (and night too Smile | :) )
 
He is an ex Codeproject Editor... time restrictions are still a problem.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 14 Jun 2005
Article Copyright 2005 by Brian Delahunty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid