For those of us who are not exposed to Unix at work or when we were young, regular expressions (RegEx) may not be second nature to us. I fall into that camp. So here is my one bit of RegEx advice for today. If you want to make your expression case insensitive all you have to do is add the case insensitive flag, (?i). For Example:
DOES NOT MATCH – SmartTerm Server and smartterm server
MATCHES – SmartTerm Server and (?i)smartterm server
And to make testing easier on you, here is a quick regex test app:
RegExTesterForm.cs
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RegExTest
{
public partial class RegExTesterForm : Form
{
public RegExTesterForm()
{
InitializeComponent();
Check();
}
private void txtTest_TextChanged(object sender, EventArgs e)
{
Check();
}
private void txtRegEx_TextChanged(object sender, EventArgs e)
{
Check();
}
private void Check()
{
try
{
Regex MyRegex = new Regex(txtRegEx.Text, RegexOptions.Singleline);
Match MyMatch = MyRegex.Match(txtTest.Text);
if (MyMatch.Success)
{
lblResult.Text = "Match Found";
lblResult.ForeColor = Color.Green;
}
else
{
lblResult.Text = "Match Not Found";
lblResult.ForeColor = Color.Red;
}
}
catch
{
lblResult.Text = "Invalid RegEx";
lblResult.ForeColor = Color.Red;
}
}
}
}
RegExTesterForm.Designer.cs
namespace RegExTest
{
partial class RegExTesterForm
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.TextBox txtTest;
private System.Windows.Forms.TextBox txtRegEx;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.txtTest = new System.Windows.Forms.TextBox();
this.txtRegEx = new System.Windows.Forms.TextBox();
this.lblResult = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtTest
//
this.txtTest.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtTest.Location = new System.Drawing.Point(76, 12);
this.txtTest.Name = "txtTest";
this.txtTest.Size = new System.Drawing.Size(212, 20);
this.txtTest.TabIndex = 0;
this.txtTest.TextChanged += new System.EventHandler(this.txtTest_TextChanged);
//
// txtRegEx
//
this.txtRegEx.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtRegEx.Location = new System.Drawing.Point(76, 38);
this.txtRegEx.Name = "txtRegEx";
this.txtRegEx.Size = new System.Drawing.Size(212, 20);
this.txtRegEx.TabIndex = 1;
this.txtRegEx.TextChanged += new System.EventHandler(this.txtRegEx_TextChanged);
//
// lblResult
//
this.lblResult.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lblResult.Location = new System.Drawing.Point(12, 61);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(276, 23);
this.lblResult.TabIndex = 2;
this.lblResult.Text = "Match";
this.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 15);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(58, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Test String";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 41);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(39, 13);
this.label3.TabIndex = 5;
this.label3.Text = "RegEx";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(300, 89);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.txtRegEx);
this.Controls.Add(this.txtTest);
this.Name = "Form1";
this.Text = "RegEx Tester";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}