Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Tester 1

Status
Not open for further replies.

Custom24

Programmer
Nov 27, 2001
591
GB
Hi
In VB6 I had a really useful Regular Expression add-in I got somewhere from the web

I thought I'd do my own for .net. Here is the form code. I've not made it into an add in yet, but I thought someone might be interested in it anyway...

Option Strict On
Imports System.Text.RegularExpressions
Public Class frmMain
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lblNumber As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents lstMatches As System.Windows.Forms.ListBox
Friend WithEvents txtPattern As System.Windows.Forms.TextBox
Friend WithEvents txtSource As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblNumber = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.lstMatches = New System.Windows.Forms.ListBox()
Me.txtPattern = New System.Windows.Forms.TextBox()
Me.txtSource = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'lblNumber
'
Me.lblNumber.Location = New System.Drawing.Point(392, 128)
Me.lblNumber.Name = &quot;lblNumber&quot;
Me.lblNumber.Size = New System.Drawing.Size(72, 40)
Me.lblNumber.TabIndex = 20
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 48)
Me.Label2.Name = &quot;Label2&quot;
Me.Label2.TabIndex = 19
Me.Label2.Text = &quot;Pattern&quot;
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = &quot;Label1&quot;
Me.Label1.TabIndex = 18
Me.Label1.Text = &quot;Source&quot;
'
'lstMatches
'
Me.lstMatches.Location = New System.Drawing.Point(8, 128)
Me.lstMatches.Name = &quot;lstMatches&quot;
Me.lstMatches.Size = New System.Drawing.Size(368, 251)
Me.lstMatches.TabIndex = 16
'
'txtPattern
'
Me.txtPattern.Location = New System.Drawing.Point(120, 48)
Me.txtPattern.Name = &quot;txtPattern&quot;
Me.txtPattern.Size = New System.Drawing.Size(176, 20)
Me.txtPattern.TabIndex = 15
Me.txtPattern.Text = &quot;&quot;
'
'txtSource
'
Me.txtSource.Location = New System.Drawing.Point(120, 8)
Me.txtSource.Name = &quot;txtSource&quot;
Me.txtSource.Size = New System.Drawing.Size(360, 20)
Me.txtSource.TabIndex = 14
Me.txtSource.Text = &quot;&quot;
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 405)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblNumber, Me.Label2, Me.Label1, Me.lstMatches, Me.txtPattern, Me.txtSource})
Me.Name = &quot;frmMain&quot;
Me.Text = &quot;frmMain&quot;
Me.ResumeLayout(False)

End Sub

#End Region



Private Sub MyExecute()
'takes the source text, outputs all the matches to the match list box
lstMatches.Items.Clear()
'if there is an error, add this to the list box rather than the matches
Try
Dim re As New Regex(txtPattern.Text)
Dim mchs As MatchCollection = re.Matches(txtSource.Text)
Dim mch As Match
For Each mch In mchs
lstMatches.Items.Add(mch.ToString & &quot; (&quot; & mch.Index.ToString & &quot;)&quot;)
Next
lblNumber.Text = mchs.Count.ToString & &quot; matches found&quot;
Catch ex As Exception
lstMatches.Items.Add(ex.Message)
End Try
End Sub


Private Sub txtSource_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSource.TextChanged
Call MyExecute()
End Sub


Private Sub txtPattern_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPattern.TextChanged
Call MyExecute()
End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class
 
There is a very useful tool on the web for regular expression validation. It is called RegexDesigner.NET (you can downlaod it free
Also there is a veb page that you can use to validate a regular expressions ( it also contain a number of ready made regular expressions fitted for the most common cases (e.g. email, postcodes, etc)

Camel
 
Thanks Camel :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top