|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI think that after an introduction of Sometimes you need a fast way to validate your regular expression, but you can lose a little bit time to create a simple project for This article a simple, but useful web service to validate a regular expression. The codeSo, let's stop talking and go to the code :) First of all, we need to import two namespaces: Imports System.Web.Services
Imports System.Text.RegularExpressions
Now we'll create a <WebMethod(Description:="Simple method to parse a regular _
expression over a string and return the result")> _
Public Function SimpleMatch(ByVal pattern As String,_
ByVal toMatch As String) As String
Dim mRegExp As Regex
Try
mRegExp = New Regex(pattern)
If mRegExp.IsMatch(toMatch) Then
Return "It´s match."
Else
Return "No match."
End If
Catch exp As Exception
Return "Exception occured: " & exp.ToString()
Finally
mRegExp = Nothing
End Try
End Function
The Now, we'll create a little bit more complex My suggestion is create a class inside the web service to set regular expression pattern parameters, like this: Public Class RegOptions
Public IgnoreCase As Boolean
Public ECMAScript As Boolean
Public ExplicitCapture As Boolean
Public IgnorePatternWhitespace As Boolean
Public MultiLine As Boolean
Public RightToLeft As Boolean
Public SingleLine As Boolean
End Class
Now add a Private _mOptions As RegexOptions
Private Property mOptions() As RegexOptions
Get
Return _mOptions
End Get
Set(ByVal Value As RegexOptions)
_mOptions = Value
End Set
End Property
The Private Sub DefineOptions(ByVal Options As RegOptions)
If Options.IgnoreCase Then mOptions = RegexOptions.IgnoreCase _
Or mOptions
If Options.ECMAScript Then mOptions = RegexOptions.ECMAScript _
Or mOptions
If Options.ExplicitCapture Then mOptions = RegexOptions.ExplicitCapture _
Or mOptions
If Options.IgnorePatternWhitespace Then mOptions = _
RegexOptions.IgnorePatternWhitespace _
Or mOptions
If Options.MultiLine Then mOptions = RegexOptions.Multiline _
Or mOptions
If Options.RightToLeft Then mOptions = RegexOptions.RightToLeft _
Or _mOptions
If Options.SingleLine Then mOptions = RegexOptions.Singleline _
Or mOptions
End Sub
Now we can create a <WebMethod(Description:="Parse a Regular Expression with _
a configured parameters")> _
Public Function MatchWithOptions(ByVal pattern As String, ByVal _
toMatch As String, ByVal MyOptions As RegOptions) As String
Dim mRegExp As Regex
Try
Call DefineOptions(MyOptions)
mRegExp = New Regex(pattern, mOptions)
If mRegExp.IsMatch(toMatch) Then
Return "It´s match."
Else
Return "No match."
End If
Catch exp As Exception
Return "Exception occured: " & exp.ToString()
End Try
End Function
Using the codeTo use the
I'll use it to validate an E-mail address:
When you click on the Invoke button the pattern and string are sent to Web service and then the Web service returns the result of the regular expression validation. In the example above I put a valid E-mail address, so the result is:
But when I put an invalid E-mail address the result is:
Now for testing the So we need to create a simple Windows Forms application to use both
Then add a Web reference to
Now, the
Let's add a code to call the Inside Form class, add to private properties: Private mRegExpService As RegExpService.RegExpService
Private mregOptions As RegExpService.RegOptions
The code for Private Sub btnSimpleMatch_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnSimpleMatch.Click
Try
Me.Cursor.Current = Cursors.WaitCursor
mRegExpService = New RegExpService.RegExpService()
MessageBox.Show(CType(mRegExpService.SimpleMatch( _
Me.txtSPattern.Text, _
Me.txtStoMatch.Text), String), _
Me.Text)
Catch exp As Exception
MessageBox.Show(exp.ToString, Me.Text)
Finally
Me.Cursor.Current = Cursors.Default
mRegExpService.Dispose()
End Try
End Sub
The code for Private Sub btnMatchWithOptions_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnMatchWithOptions.Click
Try
Me.Cursor.Current = Cursors.WaitCursor
mRegExpService = New RegExpService.RegExpService()
mRegOptions = New RegExpService.RegOptions()
With mRegOptions
.IgnoreCase = Me.cbxIgnoreCase.Checked
.ECMAScript = Me.cbxEMCAScript.Checked
.ExplicitCapture = Me.cbxExplicitCapture.Checked
.MultiLine = Me.cbxMultiLine.Checked
.RightToLeft = Me.cbxRightToLeft.Checked
.SingleLine = Me.cbxSingleLine.Checked
.IgnorePatternWhitespace = Me.cbxIgnorePatternWhitespace.Checked
End With
MessageBox.Show(CType(mRegExpService.MatchWithOptions( _
Me.txtMPattern.Text, _
Me.txtMtoMatch.Text, _
mRegOptions), String), _
Me.Text)
Catch exp As Exception
MessageBox.Show(exp.ToString, Me.Text)
Finally
Me.Cursor.Current = Cursors.Default
mRegExpService.Dispose()
End Try
End Sub
The GUI application now has the entire code to use both
The simple expression above will validate a string that starts with a character between "a" and "z" always uppercase. The text to match is "A" (uppercase) so the result is:
But if you check the regular expression option IgnoreCase, the result will be "It's match", 'cause the pattern will ignore case when validating a string.
Points of interestThis is a small Web service to provide a fast and simple way to validate regular expressions. If you like to use a more robust GUI application to build and test regular expressions, I recommend the Expresso Tool provided by Jim Hollenhorst available in CodeProject. Well, as I said before: "I think that when you use Web Services technologies, there are no limits for your applications, you just need to be creative ;-)". History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||