This article will attempt to explain the steps involved with creating a WinForm application that will validate regular expressions using the System.Text.RegularExpression namespace. The application will accept two inputs. The first input will be a string to search. The second input will be the regular expression that serves as our search criteria.
Let me take the time to say I seen an application written by another developer that served the same purpose as this application. Unfortunately, I do not remember that person's name, but he or she deserves credit for the idea behind this application. I wrote the code for this application because I thought it would be a good application to learn with.
This article is written primarily for beginners. It assumes the reader has a fundamental knowledge of regular expressions, Visual Studio.NET, and VB.NET.
Let's begin!
Step 1 Create the project
Open Visual Studio.NET and create a Visual Basic windows application project called "RegXP".
Step 2 Configure the form
Using the property explorer in the IDE set the properties of the form as follows:
| Property | Value |
| FormBorderStyle | FixedToolWindow |
| Name | frmMain |
| Text | Regular Expressions Checker |
| TopMost | True |
| Size | 408, 402 |
|
NOTE: Right-click on the RegXP project. Select properties, and change the start-up object from form1 to frmMain.
NOTE: Some properties have been provided for aesthetic purposes, but they can be set at your discretion.
Step 3 Add Controls to form
The following controls will be added to frmMain:
| Control Name | Type |
| txtSearchString | Textbox |
| txtRegExp | Textbox |
| lblSS | Label |
| lblRE | Label |
| gbOptions | GroupBox |
| cbIgnoreCase | Checkbox |
| cbRightToLeft | Checkbox |
| btnTest | Button |
| btnMatch | Button |
| btnCancel | Button |
| lvResults | Listview |
|
The properties for the aforementioned controls are configured as follows:
txtSearchString
| Property | Value |
| Location | 64, 16 |
| Name | txtSearchString |
| Size | 328, 20 |
| TabIndex | 0 |
| Text | "" |
| BorderStyle | FixedSingle |
|
txtRegExp
| Property | Value |
| Location | 64, 48 |
| Name | txtRegExp |
| Size | 328, 20 |
| TabIndex | 1 |
| Text | "" |
| BorderStyle | FixedSingle |
|
This control needs a little validation to ensure the regular expressions are not JavaScript style regular expressions. Add the following code to a procedure that handles the leave event of txtRegExp:
'check to see if the user attempted to add the forward slashes to the regular
' expression
If txtRegExp.Text.StartsWith("/") = True And txtRegExp.Text.EndsWith( _
"/") = True Then
'Remove the slash at the end of the string
txtRegExp.Text = txtRegExp.Text.Remove(txtRegExp.Text.Length - 1, 1)
'Remove the slash at the beginning of the string
txtRegExp.Text = txtRegExp.Text.Remove(0, 1)
End If
|
lblSS
| Property | Value |
| Name | lblSS |
| Text | "String:" |
| TabIndex | 3 |
| Size | 37, 13 |
| Location | 8, 16 |
|
lblRE
| Property | Value |
| Name | lblRE |
| Text | "Reg Exp:" |
| TabIndex | 4 |
| Size | 51, 13 |
| Location | 8, 48 |
| Autosize | True |
|
lvResults
| Property | Value |
| Multiselect | False |
| Size | 376, 144 |
| Location | 8, 152 |
|
We need to add some initialization code for lvResults in the constructor of the form. Add the following code after the call to InitializeComponent() in the New() procedure of the form:
'Add columns to the listview control
LvResults.Columns.Add("Position", -2, HorizontalAlignment.Left)
LvResults.Columns.Add("Value", -2, HorizontalAlignment.Left)
LvResults.FullRowSelect = True
LvResults.GridLines = True
LvResults.View = View.Details
|
gbOptions
| Property | Value |
| Text | "Options" |
| Size | 328, 48 |
| Location | 64, 80 |
|
Note: The following controls need to be added to gbOptions. This can be accomplished by selecting gbOptions, then single clicking on the control you would like to add in the toolbox. Lastly, single click within the pre-selected gbOptions and VS.NET will automatically add the control to the control collection of gbOptions.
cbIgnoreCase
| Property | Value |
| Text | "Ignore Case" |
| Location | 16, 16 |
|
cbRightToLeft
| Property | Value |
| Text | "Right to Left" |
| Location | 220, 16 |
|
Now, for the buttons...
btnCancel
| Property | Value |
| DialogResult | Cancel |
| Location | 137, 336 |
| Text | "C&lose" |
|
Write a procedure to handle the OnClick event of the btnCancel control contains the following code:
btnMatch
| Property | Value |
| Location | 317, 336 |
| Text | "&Match" |
|
Write a procedure to handle the OnClick event of the btnMatch control contains the following code:
Dim m As System.Text.RegularExpressions.Match
Dim lvi As ListViewItem
Dim re As System.Text.RegularExpressions.Regex
'Make sure our program doesn't act ugly
'when we get an invalid regular expression
Try
re = New System.Text.RegularExpressions.Regex(txtRegExp.Text)
Catch
MsgBox("Invalid Regular Expression")
Exit Sub
End Try
Dim opt As New System.Text.RegularExpressions.RegexOptions()
If txtRegExp.Text.Length <= 0 Then Exit Sub
If txtSearchString.Text.Length <= 0 Then Exit Sub
If cbIgnoreCase.Checked = True And cbRightToLeft.Checked = True Then
m = re.Match(txtSearchString.Text, txtRegExp.Text, _
opt.IgnoreCase Or opt.RightToLeft)
ElseIf cbIgnoreCase.Checked = True And cbRightToLeft.Checked = False Then
m = re.Match(txtSearchString.Text, txtRegExp.Text, opt.IgnoreCase)
ElseIf cbIgnoreCase.Checked = False And cbRightToLeft.Checked = True Then
m = re.Match(txtSearchString.Text, txtRegExp.Text, opt.RightToLeft)
Else
m = re.Match(txtSearchString.Text, txtRegExp.Text, opt.None)
End If
lvResults.Items.Clear()
If m.Captures.Count > 0 Then
Do While m.Success = True
lvi = New ListViewItem(m.Index)
lvi.SubItems.Add(m.Value)
lvResults.Items.Add(lvi)
m = m.NextMatch()
Loop
Else
lvi = New ListViewItem("-1")
lvi.SubItems.Add("No matches were found.")
lvResults.Items.Add(lvi)
End If
|
btnTest
| Property | Value |
| Location | 224, 336 |
| Text | "&Test" |
|
Write a procedure to handle the OnClick event of the btnTest control contains the following code:
Dim re As System.Text.RegularExpressions.Regex
'Make sure our program doesn't act ugly
'when we get an invalid regular expression
Try
re = New System.Text.RegularExpressions.Regex(txtRegExp.Text)
Catch
MsgBox("Invalid Regular Expression")
Exit Sub
End Try
MsgBox(re.IsMatch(txtSearchString.Text))
|
We're done! Click run, and enjoy your own regular expressions checker.
Part two of this article will explain how to make this application save regular expressions.